U E D R , A S I H C RSS

하노이탑/한유선김민경

~cpp 
#include<stdio.h>

int cnt = 0;
void movehanoi(char a, char b, char c, int n);

void movehanoi(char from, char temp, char to, int n)
{
  if(n==1){
        ++cnt;
        printf("%5d:말뚝 %c 에서 말뚝 %c 로 원반 %d 을 이동 \n", cnt, from, to, 1);
}
  else{
        movehanoi(from, to, temp, n-1);
        ++cnt;
        printf("%5d:말뚝 %c 에서 말뚝 %c 로 원반 %d 을 이동 \n", cnt, from, to, n);
        movehanoi(temp, from, to, n-1);
}

}




//함수시작
int main(void)
{
       int n;

       printf("하노이 탑에서 옮기려는 원반의 수는?>");
       scanf("%d", &n);

       movehanoi('A', 'B', 'C', n);

       return 0;
}
//함수종료
Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:31:29
Processing time 0.0119 sec