Posted 2020-10-10Updated 2020-10-11Data Structure / C2 minutes read (About 264 words)DS_1_1_Recursion数据结构_chapter 1_1.1_递归(recursion)1. 递归实现阶乘12345678910111213141516#include<stdio.h>double factorial(unsigned int number) //数据类型 double 可用于阶乘运算较宽数值范围乘积的存储{ if(number<=0){ //此处应修改为: number<=1 ,若为 number<=0, 则会重复一次乘1操作 return 1; } return number*factorial(number-1); } int main(){ unsigned int number; scanf("%d",&number); printf("%d的阶乘结果为:%f\n",number,factorial(number)); return 0; } 2. 递归实现斐波那契数列前 ‘n’ 项输出123456789101112131415161718192021222324#include<stdio.h>int fibonaci(int i){ if(i==1) //数列第一项 { return 0; } if(i==2) //数列第二项 { return 1; } return fibonaci(i-1) + fibonaci(i-2);}int main(){ int number,i; scanf("%d",&number); for(i=1;i<=number;i++) { printf("%d\t\n",fibonaci(i)); } return 0;} 3. 递归实现从’1~n’整型数据顺序输出非递归方法: 1234567891011121314151617#include<stdio.h>void PrintN(int N){ int i; for(i=1;i<=N;i++) printf("%d\n",i); return;}int main(){ int N; scanf("%d",&N); PrintN(N); return 0;} 递归方法: 123456789101112131415#include<stdio.h>void PrintN(int N){ if(N>0){ PrintN(N-1); printf("%d\n",N); }}int main(){ int N; scanf("%d",&N); PrintN(N); return 0;} or 1234567891011121314151617#include<stdio.h>void PrintN(int N);int main(){ int N; scanf("%d",&N); PrintN(N); return;}void PrintN(int N){ if(N>0){ PrintN(N-1); printf("%d\n",N); }}