题目地址:点击打开链接
思路:错排公式的简单运用,刚开始写了个错误代码,wrong了无数发
错误代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; ll a[25]; void init() { int i,j; for(i=2; i<=20; i++) { int sum = 1; for(j=1; j<i; j++) { sum *= j; } a[i] = sum; } } int main() { int n; init(); while(scanf("%d",&n) != EOF) { printf("%I64d\n",a[n]); } return 0; }
AC代码:
#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <queue> #include <stack> #include <map> #include <cstring> #include <climits> #include <cmath> #include <cctype> typedef long long ll; using namespace std; ll a[25]; void init() { int i; a[1] = 0; a[2] = 1; for(i=3; i<=20; i++) { a[i] = (i-1) * (a[i-2] + a[i-1]); } } int main() { int n; init(); while(scanf("%d",&n) != EOF) { printf("%I64d\n",a[n]); } return 0; }