C ++ Programming Exercise 1-who did I get?

Source: Internet
Author: User

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 ."
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.