HDU2048 神,上帝以及老天爷 错排

http://acm.hdu.edu.cn/showproblem.php?pid=2048

这是一道错排的题目

错排如下:http://baike.baidu.com/link?url=U2_H-4i4A7m-AtlBtXGrTSqE2yzRzIsvkM3jGgrkdrrmncvx_1AJG075d3jgoPnl9dggD4dYBtOXhifpYMBGXK

公式:f(n) = (n-1)(f(n-1)+f(n-2))

求出错排情况除以总情况即可

 1 #include<iostream>
 2 #include<cmath>
 3 #include<iomanip>
 4 using namespace std;
 5 
 6 
 7 int main()
 8 {
 9           int n;
10           cin>>n;
11           while(n--)
12           {
13                     int t;
14                     cin>>t;
15                     long long a[20];
16                     a[1] = 0;
17                     a[2] = 1;
18                     long long s = 1;
19                     for(int i = 3;i<=t;i++)
20                     {
21                               a[i] = (i-1)*(a[i-1]+a[i-2]);
22                     }
23                     for(int i = 1;i<=t;i++)
24                               s*=i;
25                     cout<<fixed<<setprecision(2)<<(double)a[t]*100/s<<"%"<<endl;
26           }
27 
28 }
原文地址:https://www.cnblogs.com/qlky/p/4959374.html