#include <iostream> using namespace std; long long a[21]; long long b[21]; int main() { int C; a[1] = 0; a[2] = 1; b[1] = 1; b[2] = 2; for (long i = 3; i <= 20; i++) { a[i] = (i - 1)*(a[i - 1] + a[i - 2]); b[i] = i*b[i - 1]; } while (cin >> C) { while (C--) { int n; cin >> n; printf("%.2lf", 100.0*a[n] / b[n]); cout << "%" << endl; } } }
1--n
第一个位置是2--n,n-1种
第二个位置是1,剩余数排列 a(n-2)
第二个位置不是1,剩余数排列a(n-1)
a(n) = (n-1)*(a(n-2) + a(n-1))
http://acm.hdu.edu.cn/showproblem.php?pid=2048