다음 C 언어 코드를 실행한 후 5번째 및 7번째 줄에 출력되는 문장으… | [7급 국가직 자료구조론] 21년 1회차 기출문제 상세 페이지
1[7급 국가직 자료구조론] 21년 1회차
다음 C 언어 코드를 실행한 후 5번째 및 7번째 줄에 출력되는 문장으로 옳은 것은?5번째 줄 7번째 줄#include <stdio.h>
void hanoi(int n, char from, char tmp, char to) {
if( n==1 )
printf("disk 1 from %c to %c\n", from, to);
else {
hanoi(n-1, from, to, tmp);
printf("disk %d from %c to %c\n", n, from, to);
hanoi(n-1, tmp, from, to);
}
}
int main(int argc, char* argv[]) {
hanoi(3, 'A', 'B', 'C');
}
1
disk 1 from B to A disk 1 from A to C
2
disk 1 from C to B disk 2 from B to C
3
disk 2 from A to B disk 3 from A to C
4
disk 3 from A to C disk 2 from B to C
해설
다섯째 줄은 disk 1 from B to A, 일곱째 줄은 disk 1 from A to C이다.
hanoi(3, A, B, C)는 원판 2개를 A→B로 옮기고(첫째~셋째 줄), disk 3을 A→C로 옮긴 뒤(넷째 줄), 원판 2개를 B→C로 옮긴다(다섯째~일곱째 줄).
전체 출력: disk 1 from A to C
disk 2 from A to B
disk 1 from C to B
disk 3 from A to C
disk 1 from B to A
disk 2 from B to C
disk 1 from A to C
- disk 1 from C to B와 disk 2 from B to C는 각각 셋째 줄과 여섯째 줄 출력이다.
- disk 2 from A to B와 disk 3 from A to C는 각각 둘째 줄과 넷째 줄 출력이다.
- disk 3 from A to C와 disk 2 from B to C는 각각 넷째 줄과 여섯째 줄 출력이다.
오류나 개정된 내용이 보이면 알려주세요