- 在递归的时候,和数学的归纳法一致。
void func( mode) { if(endCondition) { constExpression //基本项 } else { accumrateExpreesion //归纳项 mode=expression //步进表达式 func(mode) //调用本身,递归 } }
- 回文是一种字符串,它正着读和反着读都是一样的。比如level,eye都是回文。用迭代的方法可以很快地判断一个字符串是否为回文。用递归的方法如何来实现呢
-
1 #include"iostream" 2 #include<stdio.h> 3 #include"string" 4 #define MAX 100 5 using namespace std; 6 7 8 /*这是错误的,传进str的整个string则没有办法化解成小问题,这里需要指 9 之后强行把string str 改为指针也不对,会出现异常错误。 10 int per(int n ,string str){ 11 if (n == 1 && n == 0) 12 return 1; 13 else { 14 per(n - 2, str[1]); 15 } 16 } 17 */ 18 int per(int n, char *str) { 19 if (n == 1 || n == 0) 20 return 1; 21 else { 22 if (str[0] == str[n - 1]) 23 per(n - 2, &str[1]); 24 else 25 return -1; 26 } 27 } 28 29 30 31 int main() { 32 int len; 33 char str[MAX]; 34 while(1) 35 { 36 printf("please enter the word :"); 37 //os<<s 38 scanf("%s", &str); 39 len = (int)strlen(str); 40 int result = per(len, str); 41 if (result == 1) 42 printf("true "); 43 else 44 printf("fault "); 45 46 } 47 48 system("pause "); 49 }
string 类型的指针会出现错误,现在还不知道错误在哪里疑问