求斐波那契数列
#include<bits/stdc++.h> using namespace std; int rab(int n) { if(n==1) return 1; if(n==2) return 1; else return rab(n-1)+rab(n-2); } int main() { int n; cin>>n; cout<<rab(n); }
求连续乘法
#include<iostream> using namespace std; int f(int m,int n) { if(n==m) return m; else return n*f(m,n-1); } int main() { int m,n; cin>>m>>n; cout<<f(m,n); }
把m个无区别的苹果放入n个无区别的盒子(可以空盒)
#include<iostream> using namespace std; int f(int m,int n) { if(n>m) return f(m,m); if(m==0) return 1; if(n<=0) return 0; return f(m,n-1)+f(m-n,n); } int main() { int m,n,t; cin>>t; while(t--) { cin>>m>>n; cout<<f(m,n)<<endl; } }
把m个有区别苹果放入n个无区别盒子(可以空盒)
#include<iostream> using namespace std; int f(int m,int n) { if(n==1) return 1; if(m<=0) return 0; else return n*f(m-1,n)+f(m-1,n-1); } int main() { int m,n; cin>>m>>n; cout<<f(m,n); }
标签:初体验,return,cout,递归,int,namespace,using,include From: https://www.cnblogs.com/weinan030416/p/17067323.html