根据题面可知,这道题实际上就是让我们求:当所有人都拿不到自己所对应的号时的可能性,实际上就是一个重排问题
直接上代码
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=20+5;
ll dp[N];
int n;
int t;
ll pl(int i)
{
ll sum = 1;
for(int j = 2; j <= i; j++) sum *= j;
return sum;
}
int main()
{
dp[0]=0;
dp[1]=0;
dp[2]=1;
for (int i=3; i<=N; i++)
dp[i]=(i-1)*(dp[i-1]+dp[i-2]);
while (scanf("%d",&t)!=EOF)
{
while (t--)
{
scanf("%d",&n);
printf("%.2lf%%\n",100.0*dp[n]/pl(n));
}
}
return 0;
}