1.累加
#include
#include
int addTo(int n) {
int sum;
if (n <=0) {
return 0;
}
else {
sum = addTo(n - 1) + n;
return sum;
}
}
int main() {
int sum;
sum = addTo(5);
printf("%d\r\n", sum);
}
2.汉诺塔
#include
void hanoi(int n , char A , char B , char C)
{
if(n == 1)
printf("%c---->%c\n",A,C);
else
{
hanoi(n-1,A,C,B);
printf("%c---->%c\n",A,C);
hanoi(n-1,B,A,C);
}
}
int main()
{
int n;
scanf("%d",&n);
hanoi(n,'A','B','C');
return 0;
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)