题目描述
俗话说,“不怕神一样的对手,就怕猪一样的队友。”虽然后世的历史学家们总是对修罗王的黑暗军团最终以惨败告终的原因争吵不休,但有一个原因是大家公认的,那就是邪狼把N封军事情报装在N个信封时,他居然全部都装错了信封。不管你信不信,反正我是信了,现求所有情报都装错信封共有多少种可能?
输入
一个整数N,1<N<20。
输出
一个整数,即可能数。
样例输入
2
样例输出
1
由高中数学排列组合的知识,可得递推关系式:f[n]=(n-1)*(f[n-1]+f[n-2])
1 #include <iostream> 2 3 using namespace std; 4 long long f[30]; 5 int main() 6 { 7 int n; 8 cin>>n; 9 f[2]=1; 10 f[3]=2; 11 if(n==2) 12 { 13 cout<<"1"<<endl; 14 return 0; 15 } 16 if(n==3) 17 { 18 cout<<"2"<<endl; 19 return 0; 20 } 21 for(int i=4;i<=n;i++) 22 f[i]=(i-1)*(f[i-1]+f[i-2]); 23 cout<<f[n]<<endl; 24 return 0; 25 }