遞歸在示例中的應用
阿新 • • 發佈:2017-11-06
ret 字符串 pan ever printf edi 輸入 tor string+ int ret=factorial(n);
return next;
int fun(int n,int k){ if(k==0) return 1; else if(k==1) return n; else return n*fun(n,k-1);}int main (){ int n=0; int k=0; printf("求n的k次方\n"); scanf("%d%d",&n,&k); printf("the namber is %d\n",fun(n,k)); system("pause"); return 0;}3. 寫一個遞歸函數DigitSum(n),輸入一個非負整數,返回組成它的數字之和,例如,調用DigitSum(1729),則應該返回1+7+2+9,它的和是19#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)int DigitSum(int n){ if(n>9) { return n%10+DigitSum(n/10); }}int main (){ int n=0; printf("求輸入n:\n"); scanf("%d",&n); printf("the namber is %d\n",DigitSum(n)); system("pause"); return 0;}4. 編寫一個函數reverse_string(char * string)(遞歸實現)實現:將參數字符串中的字符反向排列。要求:不能使用C函數庫中1.#include <stdio.h>#include <windows.h>#include <assert.h>#pragma waring (disable:4996)void reverse_string(const char * const string){ assert(string); if(*string==‘\0‘) return ; reverse_string(string+1); putchar(*string);}int main (void){ char string[80]; gets(string); reverse_string(string); system("pause"); return 0;}5.遞歸和非遞歸分別實現strlen1.#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)//.遞歸實現strlenint My_Strlen(const char *p){ if(*p==‘\0‘) return 0; else return 1+My_Strlen(p+1);}int main (){ char *p="abcdef"; int len=My_Strlen(p); printf("%d\n",len); system("pause"); return 0;}2.#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)//.非遞歸實現strlenint My_Strlen(const char *p){ int count=0; if(*p==‘\0‘) return 0; else while(*p) { p++; count++; } return count;}int main (){ char *p="abcdef"; int len=My_Strlen(p); printf("%d\n",len); system("pause"); return 0;}6.遞歸和非遞歸分別實現求n的階乘1.#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)//用遞歸實現求n的階乘int fun(int n){ if(n<=1) return 1; else return n*fun(n-1);}int main (){ int n=0; scanf("%d",&n); int ret=fun(n); printf("the namber is %d\n",ret); system("pause"); return 0;}2.#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)//用非遞歸實現求n的階乘int fun(int n){ int sum=1; if(n<=1) return 1; else { while(n) { sum=sum*(n--); } return sum; } }int main (){ int n=0; scanf("%d",&n); int ret=fun(n); printf("the namber is %d\n",ret); system("pause"); return 0;}7.遞歸方式實現打印一個整數的每一位#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)//遞歸方式實現打印一個整數的每一位void fun(int n){ if(n>9) { fun(n/10); } printf("%d\n",n%10);}int main (){ int n=0; scanf("%d",&n); fun(n); system("pause"); return 0;}
1.遞歸和非遞歸分別實現求第n個斐波那契數。
方法1
#include <stdio.h>
#include <windows.h>
#pragma waring (disable:4996)
int factorial(int k)
{
if(k<=1)
return 1;
else
return factorial(k-2)+factorial(k-1);
}
int main ()
{
int n=0;
printf("求第幾個的斐波那契數\n");
scanf("%d",&n);
int ret=factorial(n);
printf("the namber is %d\n",ret);
system("pause");
return 0;
}
方法2
#include <stdio.h>
#include <windows.h>
#pragma waring (disable:4996)
int fun(int n)
{
int p=1;
int pp=1;
int next=1;
while(n-->2)
{
p=pp;
pp=next;
next=p+pp;
}
return next;
}
int main ()
{
int n=0;
printf("求第幾個的斐波那契數\n");
scanf("%d",&n);
int ret=fun(n);
printf("the namber is %d\n",ret);
system("pause");
return 0;
}
2.編寫一個函數實現n^k,使用遞歸實現分析:n的k次=n*n*n*n*……*n(k個n),用遞歸的思想來講,就等於n*(n的(k-1)次方)#include <stdio.h>#include <windows.h>#pragma waring (disable:4996)遞歸在示例中的應用