69,371
社区成员
发帖
与我相关
我的任务
分享
#include <iostream>
using namespace std;
//递归解决汉诺塔问题, 从from上移动n个盘子到to上
//n表示盘子的个数
void hannuo(char from, int n, char to, char temp)
{
if(n > 0)
{
hannuo(from, n-1, temp, to);
cout << "移动第" << n << "个盘子:" << "从" << from << "到" << to << endl;
hannuo(temp, n-1, to, from);
}
}
#include<stdio.h>
void reverse(char *s)
{
if(s[0]=='\0') return; // 如果是空字符串就退出
else
{
reverse(&s[1]); // 如果原字符串是"abc", 那么第一次递归的时候传入的参数就是"bc"
putchar(s[0]); // 输出s中的第一个字符a
}
}
int main()
{
char sentence[80];
void reverse(char *);
printf("Enter a line of text:\n");
gets(sentence);
printf("\nThe line printed backwards is:\n");
reverse(sentence);
printf("\n");
return 0;
}