程序代码:
#include<stdlib.h>
#include<string.h>
#include<stdio.h>
//递归实现n!
int n(int element)
{
if(0==element)
return 1;
return element*n(element-1);
}
//递归实现0-n的和
int sub_sum(int element)
{
if(0==element)
return 0;
return element+sub_sum(element-1);
}
//递归实现斐波那契
int Fibonacci(int element)
{
if(element<=1)
return element;
return Fibonacci(element-1)+Fibonacci(element-2);
}
//递归实现二进制转换
void binary(int element)
{
if(element==0)
return;
binary(element/2);
printf("%d",element%2);
}
//递归实现各个位数字之和
int Sum_bit(int element)
{
if(element==0)
return 0;
return element%10+Sum_bit(element/10);
}
//递归实现各个位数字乘积
int Pro_bit(int element)
{
if(element==0)
return 1;
return element%10*Pro_bit(element/10);
}
//递归实现单词逆置
void inversion(char *p,int low,int high)
{
if(low<high)
{
char t=*(p+low);
*(p+low)=*(p+high);
*(p+high)=t;
inversion(p,low+1,high-1);
}
else
return;
}
int main(int argc, const char *argv[])
{
int element;
printf("please input element:");
scanf("%d",&element);
//递归实现n!
int num=n(element);
printf("%d\n",num);
//递归实现0-n和
num=sub_sum(element);
printf("%d\n",num);
//递归实现斐波那契
num=Fibonacci(element);
printf("斐波那契数列第%d项为: %d\n",element,num);
//递归实现二进制转换
binary(element);
puts("");
//递归实现各个位数字之和
int sum=Sum_bit(element);
printf("%d\n",sum);
//递归实现各个位数字乘积
num=Pro_bit(element);
printf("%d\n",num);
char str[20]="";
printf("please input string:");
gets();
gets(str);
int len=strlen(str);
//递归实现单词逆置
inversion(str,0,len-1);
puts(str);
return 0;
}
运行结果: