[정보처리기사] 2022 C 프로그래밍 언어 문제
1회-7번) 다음 C언어로 구현된 프로그램을 분석하여 5를 입력했을 때 그 실행 결과를 쓰시오.
#inlcude <stdio.h>
int func(int a) {
if(a<=1) return 1;
return a * func(a-1);
}
int main() {
int a;
scanf("%d", &a);
printf("%d", func(a));
}
답:
120
1회-15번) 다음은 정수를 역순으로 출력하는 C언어 프로그램이다. 예를 들어 1234의 역순은 4321이다. 단, 1230처럼 0으로 끝나는 정수는 고려하지 않는다. 프로그램을 분석하여 괄호에 들어갈 알맞은 연산자를 쓰시오.
#include <stdio.h>
int main() {
int number = 1234;
int div = 10, result = 0;
while(number ( 1 ) 0) {
result = result * div;
result = result + number ( 2 ) div;
number = number ( 3 ) div;
}
printf("%d", result);
}
답:
(1) >
(2) %
(3) /
1회-17번) 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오.
#include <stdio.h>
int isPrime(int number) {
for(int i=2; i<number; i++)
if(number%i == 0) return 0;
return 1;
}
int main() {
int number = 13195;
int max_div = 0;
for(int i=2; i<number; i++)
if(isPrime(i)==1 && number%i == 0) max_div = i;
printf("%d", max_div);
}
답: 29
main에 있는 if문 안의 조건 두 가지를 보자.
먼저, number%i ==0 인 조건을 먼저 보자면,
number = 13195 = 5* 7 * 13 * 29이므로 i가 될 수 있는 수는 5, 7, 13, 29이다.
isPrime(i) == 1에서 5, 7, 13, 29는 소수(1과 자기 자신만 약수로 갖는 수)이므로 무조건 return이 1이다.
따라서 for문 내에서 max_div는 5가 되었다가 7이 되었다가 13이 되었다가
최종적으로는 29가 될 것이다.
2회-8번) 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오.
#include <stdio.h>
struct A {
int n;
int g;
};
main() {
struct A st[2];
for(int i=0; i<2; i++) {
st[i].n = i;
st[i].g = i + 1;
}
printf("%d", st[0].n + st[1].g);
}
답: 2
아래는 자바를 배운 사람으로서 내 맘대로 자바식 풀이를 적어둔 것이다. (정확한 표현 아닐 수 있음)
struct A st[2]는 크기가 2이고 타입이 A 구조체인 st 배열을 선언한 것이다.
st[0]에도 멤버 변수인 n과 g가 존재하고,
st[1]에도 멤버 변수인 n과 g가 존재한다.
for문을 통해서 st[0].n = 0, st[1].g = 2임을 알 수 있다.
2회-15번) 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오.
#include <stdio.h>
int main() {
char *p1 = "2022";
char *p2 = "202207";
int a = len(p1);
int b = len(p2);
printf("%d", a + b);
}
int len(char *p) {
int r = 0;
while(*p != '\0') {
p++;
r++;
}
return r;
}
답: 10

2회-16번) 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오.
#include <stdio.h>
int main() {
int a[4] = {0, 2, 4, 8};
int b[3];
int *p;
int sum = 0;
for (int i=1; i<4; i++) {
p = a + i;
b[i-1] = *p - a[i-1];
sum = sum + b[i-1] + a[i];
}
printf("%d", sum);
}
답: 22

3회-1번) 다음 C언어로 구현된 프로그램을 분석하여 배열 <mines>의 각 칸에 들어갈 값을 쓰시오.
#include <stdio.h>
main() {
int field[4][4] = {{0, 1, 0, 1}, {0, 0, 0, 1}, {1, 1, 1, 0}, {0, 1, 1, 1}};
int mines[4][4] = {{0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0}};
int w = 4, h = 4;
for(int y = 0; y<h; y++) {
for(int x=0; x < w; x++) {
if(field[y][x] == 0) continue;
for(int j = y-1; j<=y+1; j++) {
for(int i=x-1; i<=x+1; i++) {
if(chkover(w, h, j, i) == 1)
mines[j][i] += 1;
}
}
}
}
}
int chkover(int w, int h, int j, int i) {
if(i>=0 && i<w && j>=0 && j<h) return 1;
return 0;
}
배열 <field>
0 | 1 | 0 | 1 |
0 | 0 | 0 | 1 |
1 | 1 | 1 | 0 |
0 | 1 | 1 | 1 |
배열 <mines>
답: 아직 안 풀었음
3회-13번) 다음 C언어로 구현된 프로그램을 분석하여 그 실행 결과를 쓰시오.
#include <stdio.h>
main() {
int s, el = 0;
for(int i=6; i<=30; i++) {
s = 0;
for(int j=1; j<=i/2; j++) {
if(i%j == 0) s = s + j;
}
if(s == i) {
el++;
}
}
printf("%d", el);
}
답: 2
for(int j=1; j<=i/2; j++) if(i%j == 0)에서 i는 완전수임을 알 수 있다.
즉, 자기 자신을 제외한 양의 양수를 더했을 때 자기 자신이 되는 수를 찾으면 된다.
6 이상 30이하인 수 중 완전수는 6과 28이 있다.
따라서 el은 2번 증가하여 2가 된다.