Description
After the end of the examination, long Shao and Cang Hai pinned their hopes on the textbooks. Who knows the evil invigilators want every student to put the books on the podium, longshao was unhappy. After the examination, he took the book and left. After returning to the dormitory, he found that he was not his own book. Then he suddenly fell into meditation, how many situations do the 30 people take the test get the wrong book like him? (Assume that every student has a book and only one book)
Input
The first act of the input data is an integer T, representing the number of test data. The next T row contains an integer N, representing the number of middle school students in a classroom.
Output
For each group of test data, an integer is output, indicating the total number of wrong books
Sample Input
3
1
2
3
Sample Output
0
1
2
Hint
Http://baike.baidu.com/view/668994.htm
Reference Code
View plaincopy to clipboardprint?
# Include <iostream>
Using namespace std;
Const int N = 31;
Const int M = 50;
Int main (){
Int cases, n, I, j, map [N] [M];
For (I = 0; I <M; ++ I ){
Map [1] [I] = 0;
}
For (I = 2; I <N; I ++ ){
For (j = 0; j <M; ++ j ){
Map [I] [j] = map [I-1] [j] * I;
}
If (I % 2 = 0 ){
Map [I] [0] + = 1;
} Else {
Map [I] [0]-= 1;
}
For (j = 0; j <M; ++ j ){
If (map [I] [j]> 9 ){
Map [I] [j + 1] + = map [I] [j]/10;
Map [I] [j] % = 10;
} Else if (map [I] [j] <0 ){
Map [I] [j] + = 10;
Map [I] [j + 1]-= 1;
}
}
}
Cin> cases;
While (cases --){
Cin> n;
If (n = 1 ){
Cout <0 <endl;
Continue;
}
Bool B = false;
For (I = M-1; I> = 0; -- I ){
If (map [n] [I]> 0 | B ){
Cout <map [n] [I];
B = true;
}
}
Cout <endl;
}
Return
# Include <iostream>
Using namespace std;
Const int N = 31;
Const int M = 50;
Int main (){
Int cases, n, I, j, map [N] [M];
For (I = 0; I <M; ++ I ){
Map [1] [I] = 0;
}
For (I = 2; I <N; I ++ ){
For (j = 0; j <M; ++ j ){
Map [I] [j] = map [I-1] [j] * I;
}
If (I % 2 = 0 ){
Map [I] [0] + = 1;
} Else {
Map [I] [0]-= 1;
}
For (j = 0; j <M; ++ j ){
If (map [I] [j]> 9 ){
Map [I] [j + 1] + = map [I] [j]/10;
Map [I] [j] % = 10;
} Else if (map [I] [j] <0 ){
Map [I] [j] + = 10;
Map [I] [j + 1]-= 1;
}
}
}
Cin> cases;
While (cases --){
Cin> n;
If (n = 1 ){
Cout <0 <endl;
Continue;
}
Bool B = false;
For (I = M-1; I> = 0; -- I ){
If (map [n] [I]> 0 | B ){
Cout <map [n] [I];
B = true;
}
}
Cout <endl;
}
Return
Author: "ice is cold: read more, write more, and sleep more ."