정보처리기사
스터디) 2024 정처기 실기 1회 프로그래밍 풀기
초비비
2024. 10. 4. 18:57
2024 정처기 실기 1회 정리
https://chobopark.tistory.com/476 문제 복원 블로그
문제1. 싱글톤 패턴
class Connection {
private static Connection _inst = null;
private int count = 0;
static public Connection get() {
if(_inst == null) {
_inst = new Connection();
return _inst;
}
return _inst;
}
public void count() {
count++;
}
public int getCount() {
return count;
}
}
public class main {
public static void main(String[] args) {
Connection conn1 = Connection.get();
conn1.count();
Connection conn2 = Connection.get();
conn2.count();
Connection conn3 = Connection.get();
conn3.count();
conn1.count();
System.out.print(conn1.getCount());
}
}
- 객체를 단 한번만 생성하여 계속 사용
- 객체 _inst = conn1 =conn2 =conn3
- count = 0 → 1 → 2 → 3 → 4
문제2. 시프트
#include <stdio.h>
int main() {
int v1 = 0, v2 = 35, v3 = 29;
if(v1 > v2 ? v2 : v1) {
v2 = v2 << 2;
}else{
v3 = v3 << 2;
}
printf("%d", v2+v3);
}
- 29를 2진수로 바꾸고 << 2 시프트 하면 116
- https://sf2020.tistory.com/29 시프트에 관련된 내용 블로그
문제3. 포인터
#include <stdio.h>
#include <string.h>
void reverse(char* str){
int len = strlen(str);
char temp;
char*p1 = str;
char*p2 = str + len - 1;
while(p1<p2){
temp = *p1;
*p1 = *p2;
*p2 = temp;
p1++;
p2--;
}
}
int main(int argc, char* argv[]){
char str[100] = "ABCDEFGH";
reverse(str);
int len = strlen(str);
for(int i=1; i<len; i+=2){
printf("%c",str[i]);
}
printf("\n");
return 0;
}
- *p1에서 * : 메모리 주소의 값을 반환
- 인덱스 : 0,7 / 1,6 / 2,5 / 3,4 끼리 자리 바꿈
문제4. 실행 순서
class Parent {
int x, y;
Parent(int x, int y) { ①
this.x=x;
this y=y;
}
int getT() { ②
return x*y;
}
}
class Child extend Parent {
int x;
Child (int x) { ③
super(x+1, x);
this.x=x;
}
int getT(int n){ ④
return super.getT()+n;
}
}
class Main {
public static void main(String[] args) { ⑤
Parent parent = new Child(3); ⑥
System.out.println(parent.getT()); ⑦
}
}
- ① : 생성자 , ② : 메서드, ③ : 생성자, ④ : 메서드, ⑤ : 메인
- 매개변수 유무 체크 할 것!
문제5. 구조체
#include <stdio.h>
typedef struct{
int accNum;
double bal;
}BankAcc;
double sim_pow(double base, int year){
int i;
double r = 1.0;
for(i=0; i<year; i++){
r = r*base;
}
return r;
}
void initAcc(BankAcc *acc, int x, double y){
acc -> accNum = x;
acc -> bal = y;
}
void xxx(BankAcc *acc, double *en){
if (*en > 0 && *en < acc -> bal) {
acc -> bal = acc -> bal-*en;
}else{
acc -> bal = acc -> bal+*en;
}
}
void yyy(BankAcc *acc){
acc -> bal = acc -> bal * sim_pow((1+0.1),3);
}
int main(){
BankAcc myAcc;
initAcc(&myAcc, 9981, 2200.0);
double amount = 100.0;
xxx(&myAcc, &amount);
yyy(&myAcc);
printf("%d and %.2f", myAcc.accNum, myAcc.bal);
return 0;
}
- 구조체 : 서로 다른 데이터 타입의 묶음
- 화살표 연산자 (→) : 구조체 포인터 변수가 구조체 멤버에 접근할 때 사용하는 연산자
- &myAcc : myAcc의 주소값
문제6. 반복
a = ["Seoul", "Kyeonggi", "Incheon", "Daejun", "Daegu", "Pusan"]
str = "S"
for i in a:
str = str + i[1]
print(str)
문제7. 상속
class classOne {
int a, b;
public classOne(int a, int b) {
this.a = a;
this.b = b;
}
public void print() {
System.out.println(a + b);
}
}
class classTwo extends classOne {
int po = 3;
public classTwo(int i) {
super(i, i+1);
}
public void print() {
System.out.println(po*po);
}
}
public class main {
public static void main(String[] args) {
classOne one = new classTwo(10);
one.print();
}
}
- 상속 주의
문제8. 시저 암호 알고리즘
#include<stdio.h>
#include<ctype.h>
int main(){
char*p = "It is 8";
char result[100];
int i;
for(i=0; p[i]!='\0'; i++){
if(isupper(p[i]))
result[i] = (p[i]-'A'+5)% 25 + 'A';
else if(islower(p[i]))
result[i] = (p[i]-'a'+10)% 26 + 'a';
else if(isdigit(p[i]))
result[i] = (p[i]-'0'+3)% 10 + '0';
else if(!(isupper(p[i]) || islower(p[i]) || isdigit(p[i])))
result[i] = p[i];
}
result[i] = '\0';
printf("%s\n",result);
return 0;
}
- 아스키코드 값 외우기
- A : 65, Z : 90, a : 97, z : 122, 0 : 48, 9 : 57
📀 도움이 되었던 유튜브 문제 해설
https://www.youtube.com/watch?v=0sWcTYhi0MQ&t=4s
https://www.youtube.com/watch?v=ZPZhaeiyGZM&t=1280s
https://www.youtube.com/watch?v=ps0HOFDqJck