정보처리기사
스터디) 2024 정처기 실기 2회 프로그래밍 풀기
초비비
2024. 10. 7. 13:51
2024 정처기 실기 2회 정리
https://chobopark.tistory.com/483#google_vignette 문제 복원 블로그
문제1. 객체
class Main {
public static void main(String[] args) {
int[] a = new int[]{1, 2, 3, 4};
int[] b = new int[]{1, 2, 3, 4};
int[] c = new int[]{1, 2, 3};
check(a, b);
check(a, c);
check(b, c);
}
public static void check(int[] a, int[] b) {
if (a==b) {
System.out.print("O");
}else{
System.out.print("N");
}
}
}
- new : 메모리 따로 잡음
- 객체 개념, static 알아둘 것
- ==, equals() 차이
- == : 같은 메모리
- equals() : 내용 비교
문제2. f-string
def fnCalculation(x,y):
result = 0;
for i in range(len(x)):
temp = x[i:i+len(y)]
if temp == y:
result += 1;
return result
a = "abdcabcabca"
p1 = "ab";
p2 = "ca";
out = f"ab{fnCalculation(a,p1)}ca{fnCalculation(a,p2)}"
print(out)
- f-string : 문자열 앞에 f를 넣고 각 출력할 변수를 { }로 중괄호 처리
- x[i:i+len(y)]
- x[0 : 2] = ab
- x[1 : 3] = bd
- x[2 : 4] = dc
문제3. 포인터
#include <stdio.h>
int main() {
int arr[3][3] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
int* parr[2] = {arr[1], arr[2]};
printf("%d", parr[1][1] + *(parr[1]+2) + **parr);
return 0;
}
- *(parr[1]+2) : parr포인터에서 +2 = parr[1][2]
- **parr = *parr[0] = {4,5,6}
문제4. 인터페이스
class Main {
public static void main(String[] args) {
int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9};
ODDNumber OE = new ODDNumber();
System.out.print(OE.sum(a, true) + ", " + OE.sum(a, false));
}
}
interface Number {
int sum(int[] a, boolean odd);
}
class ODDNumber implements Number {
public int sum(int[] a, boolean odd) {
int result = 0;
for(int i=0; i < a.length; i++){
if((odd && a[i] % 2 != 0) || (!odd && a[i] % 2 == 0))
result += a[i];
}
return result;
}
}
- 인터페이스 : 클래스가 강제로 구현하게 끔 만든 형태
- || : 하나라도 True면 True
문제5. 포인터, 주소
#include <stdio.h>
#include <string.h>
void sumFn(char* d, const char* s) {
while (*s) {
*d = *s;
d++;
s++;
}
*d = '\0';
}
int main() {
const char* str1 = "first";
char str2[50] = "teststring";
int result=0;
sumFn(str2, str1);
for (int i = 0; str2[i] != '\0'; i++) {
result += i;
}
printf("%d", result);
return 0;
}
- sunFn(str2, str1) : first, teststring에 대한 주소
- d++, s++ : 메모리 하나씩 증가
- *s : f i r s t \0
- *d : t e s t s t r i n g \0
문제7. 문자열, 재귀
class Main {
public static void main(String[] args) {
String str = "abacabcd";
boolean[] seen = new boolean[256];
System.out.print(calculFn(str, str.length()-1, seen));
}
public static String calculFn(String str, int index, boolean[] seen) {
if(index < 0) return "";
char c = str.charAt(index);
String result = calculFn(str, index-1, seen);
if(!seen[c]) {
seen[c] = true;
return c + result;
}
return result;
}
}
- alculFn(str, index-1, seen) : 재귀를 호출해서 이전 문자를 처리
- if(!seen[c]) : 조건문으로 나왔던 문자인지 확인
- Fn(str, 7, seen) : c = 'd' + reuslt : Fn(str, 6, seen) : c = 'c' + reuslt : Fn(str, 5, seen) : c = 'b' + reuslt : Fn(str, 4, seen) : c = 'a'
- d+c+b+a
문제8. 지역변수
#include <stdio.h>
void swap(int a, int b) {
int t = a;
a = b;
b = t;
}
int main() {
int a = 11;
int b = 19;
swap(a, b);
switch(a) {
case 1:
b += 1;
case 11:
b += 2;
default:
b += 3;
break;
}
printf("%d", a-b);
}
- swap(a,b) : 낚시 문제, return이나 메모리 바꾸는게 없다
- case 11 : b+=2; 다음에 break;가 없기 때문에 default 까지 실행
- 주의! 지역변수, break 여부
문제9. 구조체
#include <stdio.h>
struct node {
int n1;
struct node *n2;
};
int main() {
struct node a = {10, NULL};
struct node b = {20, NULL};
struct node c = {30, NULL};
struct node *head = &a;
a.n2 = &b;
b.n2 = &c;
printf("%d\n", head->n2->n1);
return 0;
}
- node *n2 : node를 가리킬 수 있는 포인터 변수
- node : 메모리 크기 만 node 형태면 됨
- node *head : node 형 포인터 변수 선언
- = &a : a를 가리켜라
- * 의 3가지 용법
- 포인터 선언
- 태어난 놈의 값을 가져올 때
- 곱하기
- SQL에서는 모두(all)
- Python에서 **는 제곱
- head -> n2 = b, head -> n2 -> n1 = b의 n1
문제10. .split()
class Main {
public static void main(String[] args) {
String str = "ITISTESTSTRING";
String[] result = str.split("T");
System.out.print(result[3]);
}
}
- 파이썬은 모든 타입이 Class
- .split(문자) : 문자를 기준으로 배열(리스트)로 리턴
- ["I", "IS", "ES", "S", "RING"]
📀 도움이 되었던 유튜브 문제 해설
https://www.youtube.com/watch?v=0sWcTYhi0MQ&t=4s