数据结构--递归学习
程序员文章站
2022-03-31 10:29:07
...
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void Reverse(char *str)
{
if((str==NULL)||(*str=='\0'))
{
return;
}
else
{
Reverse(str+1);
printf("%c",*str);
}
}
int main(int argc, char *argv[])
{
char str[]="qwertyuiop";
Reverse(str);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int fibonacci(int n)
{
if(n==0)
{
return 0;
}
else if(n==1)
{
return 1;
}
else
{
return fibonacci(n-1)+fibonacci(n-2);
}
}
int main(int argc, char *argv[])
{
int i=0;
for(i=1;i<=10;i++)
{
printf("fibonacci(%d) is %d\n",i,fibonacci(i));
}
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int MyStrlen( const char *s)
{
if(s==NULL)
{
return -1;
}
else if(*s=='\0')
{
return 0;
}
else
{
return MyStrlen(s+1)+1;
}
}
int main(int argc, char *argv[])
{
char *str="adearqwarara";
char str1[]="adearqwarara";
printf("\"adearqwarara\" is %d\n",MyStrlen(str));
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void hanoi(int n,char a,char b,char c)
{
if(n<=0)
{
return;
}
else
{
if(n==1)
{
printf("%c->%c\n",a,c);
}
else
{
hanoi(n-1,a,c,b);
printf("%c->%c\n",a,c,b);
hanoi(n-1,b,a,c);
}
}
}
int main(int argc, char *argv[])
{
hanoi(4,'a','b','c');
return 0;
}
#include <stdio.h>
#include <stdlib.h>
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
void permutation(char *s,int b,int e)
{
if((0<=b)&&(b<=e))
{
if(b==e)
{
printf("%s\n",s);
}
else
{
int i=0;
for(i=b;i<=e;i++)
{
char c=s[b];
s[b]=s[i];
s[i]=c;
permutation(s,b+1,e);
c=s[b];
s[b]=s[i];
s[i]=c;
}
}
}
}
int main(int argc, char *argv[])
{
char s[]="adcd";
permutation(s,0,3);
return 0;
}