Files
python/TangDou/Mkx/HDU2048_DFS.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

37 lines
756 B
C++
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110;
typedef long long LL;
// TLE 超时,看来,还是递推更安全些
int n;
LL res, sum;
int st[N];
void dfs(int u) {
if (u == n + 1) {
res++;
return;
}
for (int i = 1; i <= n; i++)
if (!st[i] && u != i) {
st[i] = true;
dfs(u + 1);
st[i] = false;
}
}
int main() {
int m;
scanf("%d", &m);
while (m--) {
scanf("%d", &n);
memset(st, 0, sizeof st);
sum = 1;
dfs(1);
//总次数
for (int i = 1; i <= n; i++) sum = sum * i;
//比率
printf("%.2lf%%\n", (double)res / sum * 100);
}
return 0;
}