정보처리기사 실기C언어C - 재귀함수와 구조체난이도 5SHORT_ANSWER

정보처리기사 실기 C - 재귀함수와 구조체 기출문제 #268

문제

다음 C 코드의 실행 결과를 쓰시오.

C
#include <stdio.h> struct Point { int x, y; }; void process(struct Point p, int cnt) { if (cnt <= 0) { printf("%d ", p.x * p.y); return; } struct Point next1 = {p.x + cnt, p.y - 1}; struct Point next2 = {p.x - 1, p.y + cnt}; printf("%d ", p.x + p.y); if (cnt % 2 == 0) { process(next2, cnt - 2); process(next1, cnt - 1); } else { process(next1, cnt - 1); process(next2, cnt - 2); } } int main() { struct Point start = {2, 3}; process(start, 3); return 0; }

정답

5 7 16 8 0 12 7 10 0

5 7 16 8 0 12 7 10 05 7 16 8 0 12 7 10 05,7,16,8,0,12,7,10,0

해설

실행 순서를 정확히 추적:

  1. process({2,3}, 3): cnt=3(홀수), p.x+p.y=2+3=5 출력, next1={5,2}, next2={1,6}
  2. process({5,2}, 2): cnt=2(짝수), p.x+p.y=5+2=7 출력, next1={7,1}, next2={4,4}
  3. process({4,4}, 0): cnt=0≤0, p.xp.y=44=16 출력
  4. process({7,1}, 1): cnt=1(홀수), p.x+p.y=7+1=8 출력, next1={8,0}, next2={6,2}
  5. process({8,0}, 0): cnt=0≤0, p.xp.y=80=0 출력
  6. process({6,2}, -1): cnt=-1≤0, p.xp.y=62=12 출력
  7. process({1,6}, 1): cnt=1(홀수), p.x+p.y=1+6=7 출력, next1={2,5}, next2={0,7}
  8. process({2,5}, 0): cnt=0≤0, p.xp.y=25=10 출력
  9. process({0,7}, -1): cnt=-1≤0, p.xp.y=07=0 출력 따라서 출력 순서: 5 7 16 8 0 12 7 10 0

이런 문제 20~50개를 한 번에 풀어보세요

매번 새로 추가되는 모의고사 + 오답 자동 복습 + 회차별 실력 추적. 회원가입 후 무료 이용.