1.求n^k 递归
#include< stdio.h >
int power(int n,int k)
{
int count=0;//设置变量
if(k>0)//判断k为0则不必再进行替换下去
{
count=power(n,k-1)*n; //n^k可以看成n^(k-1)*n n^(k-1)*n=n^(k-2)*n*n 以此类推可以写出
}
else
count=1;
return count;
}
int main()
{
int n=0,k=0;
int ret=0;
scanf(“%d%d”,&n,&k);
ret=power(n,k);
printf(“ret= %d \n”,ret);
return 0 ;
}
2.#include< stdio.h >
int digitsum( unsigned int num)
{
int count=0;
if(num>9)两位数以上需要递归
{
count=num%10+digitsum(num/10);//例如1729 可看做172+9 也等于 17+2+9 以此类推递归
}
else
count=num;
return count;
}
int main()
{
unsigned int num;
int sum=0;
scanf(“%d”,&num);
sum=digitsum(num);
printf(“sum= %d \n”,sum);
return 0 ;
}
4.#include< stdio.h>
#include< string.h>
void reverse_string(char* string)
{
char temp;
int len=strlen(string);
if(len>1)
{
temp=*string;
string=(string+len-1);
*(string+len-1)=’\0’;
reverse_string(string+1);
*(string+len-1)=temp;
}
}
int main()
{
char arr[]=”abcde”;
reverse_string(arr);
printf(“%s”,arr);
return 0 ;
}