斐波那契数列(1)
#include <iostream> using namespace std; int x(int n){ if(n<=2){ return 1; }else{ return x(n-1)+x(n-2); } } int main(){ int n; cin>>n; int a = x(n); cout<<a; return 0; }
斐波那契数列(2)
#include <iostream> using namespace std; int x(int &n){ int a = n-1; int b = n-2; if(n<=2){ return 1; }else{ return x(a)+x(b); } } int main(){ int n; cin>>n; int sum = x(n); cout<<sum; return 0; }
斐波那契数列(3)
#include <iostream> using namespace std; int x(int *n){ int a = *n-1; int b = *n-2; if(*n<=2){ return 1; }else{ return x(&a)+x(&b); } } int main(){ int n; cin>>n; int sum = x(&n); cout<<sum; return 0; }
阶乘(1)
#include <iostream> using namespace std; int sum = 1; int x(int &n){ int a = n-1; sum = sum*n; if(n<=1){ return sum; }else{ return x(a); } } int main(){ int n; cin>>n; int sum = x(n); cout<<sum; return 0; }
阶乘(2)
#include <iostream> using namespace std; int sum = 1; int x(int *n){ int a = *n-1; sum = sum*(*n); if(*n<=1){ return sum; }else{ return x(&a); } } int main(){ int n; cin>>n; int sum = x(&n); cout<<sum; return 0; }
标签:std,递归函数,int,sum,namespace,using,include From: https://www.cnblogs.com/wangyihang-xh/p/17795500.html