PAT/simple simulation exercise set (1), pat simulation exercise set
B1001. the killer (3n + 1) conjecture (15)
Description:
Callatz conjecture:
For any natural number n, if it is an even number, cut it by half; if it is an odd number, cut (3n + 1) by half. In this way, we will continue to cut down and finally get n = 1 in a certain step. Karaz announced this conjecture at the world mathematician conference in 1950. It is said that at that time, the teachers and students of Yale University were mobilized to prove this seemingly silly and naive proposition, and the results made the students unwilling to study, as a result, some people say this is a conspiracy. karaz is deliberately delaying the progress of teaching and scientific research in American mathematics ......
Today's question is not to prove caraz's conjecture, but to give a given positive integer n of no more than 1000, simply count it. How many steps (cut down) Do we need to get n = 1?
Input:
Each test input contains one test case, that is, the value of natural number n.
Output:
The number of steps required for the output from n to 1.
Sample Input:
3
Sample Output:
5
1 #include <cstdio> 2 3 int main() 4 { 5 //freopen("E:\\Temp\\input.txt", "r", stdin); 6 7 int n, step = 0; 8 scanf("%d", &n); 9 10 while(n != 1) {11 if(n%2 == 0) {12 n /= 2;13 } else {14 n = (3*n+1)/2;15 }16 step++;17 }18 19 printf("%d\n", step);20 21 return 0;22 }
B1032. which excavator technology is strong (20)
Description:
PAT organized an excavator Skill Competition to show which excavator technology is strong. Please calculate the most technically competent school based on the competition results.
Input:
Enter a positive integer N of no more than 1st in row 105, that is, the number of participants. In the next N rows, each row provides information and scores of a contestant, including the number of the school it represents (from 1), and the score of the competition (percentage ), separated by spaces.
Output:
The number and total score of the school with the highest total score are given in a row, separated by spaces. The answer to the question must be unique.
Sample Input:
6
3 65
2 80
1 100
2 70
3 40
3 0
Sample Output:
2 150
1 #include <cstdio> 2 3 #define MaxSize 100010 4 5 int school[MaxSize]; 6 7 int main() 8 { 9 //freopen("E:\\Temp\\input.txt", "r", stdin);10 11 int n, schID, score;12 scanf("%d", &n);13 for(int i=0; i<n; ++i) {14 scanf("%d %d", &schID, &score);15 school[schID] += score;16 }17 18 int ID, maxscore = -1;19 for(int i=1; i<=n; ++i) {20 if(school[i] > maxscore) {21 ID = i;22 maxscore = school[i];23 }24 }25 26 printf("%d %d\n", ID, maxscore);27 }
B1011. A + B and C (15)
Description:
For the three integers A, B, and C in the given interval [-231,231], determine whether A + B is greater than C.
Input:
Enter the positive integer T (<= 10) in the first row, which is the number of test cases. Then, the T group test cases are given. Each group occupies one row and A, B, and C are given in sequence. Integers are separated by spaces.
Output:
For each group of test cases, "Case # X: true" is output in one row if A + B> C; otherwise, "Case # X: false" is output ", X indicates the number of the test case (starting from 1 ).
Sample Input:
4
1 2 3
2 3 4
2147483647 0 2147483646
0-2147483648-2147483647
Sample Output:
Case #1: false
Case #2: true
Case #3: true
Case #4: false
1 #include <cstdio> 2 3 int main() 4 { 5 //freopen("E:\\Temp\\input.txt", "r", stdin); 6 7 int T; 8 long long a, b, c; 9 scanf("%d", &T);10 for(int i=1; i<=T; ++i) {11 scanf("%lld %lld %lld", &a, &b, &c);12 if(a+b > c) {13 printf("Case #%d: true\n", i);14 } else {15 printf("Case #%d: false\n", i);16 }17 }18 19 return 0;20 }
B1016. Part A + B (15)
Description:
The "DA (one-digit integer) Section of positive integer A is defined as A new integer PA consisting of all DA in. For example, given A = 3862767, DA = 6, the "6 Part" PA of A is 66, because A has 2 6.
If A, DA, B, and DB are specified, write A program to calculate PA + PB.
Input:
Enter A, DA, B, and DB in one row, and separate them with spaces. 0 <A, B <1010.
Output:
Output the PA + PB value in a row.
Sample Input1:
3862767 6 13530293 3
Sample Output1:
399
Sample Input2:
3862767 1 13530293 8
Sample Output2:
0
1 #include <cstdio> 2 3 int main() 4 { 5 //freopen("E:\\Temp\\input.txt", "r", stdin); 6 7 int Da, Db; 8 long long pA = 0, pB = 0, A, B; 9 scanf("%lld %d %lld %d", &A, &Da, &B, &Db);10 11 while(A != 0) {12 if(A%10 == Da) {13 pA = pA*10+Da;14 }15 A /= 10;16 }17 while(B != 0) {18 if(B%10 == Db) {19 pB = pB*10+Db;20 }21 B /= 10;22 }23 24 printf("%lld\n", pA+pB);25 26 return 0;27 }
B1026. program running time (15)
Description:
To obtain the running time of a C Language Program, the common method is to call the header file time. h. The clock () function is provided to capture the time consumed from the program running to the call of clock. The unit of time is clock tick, that is, "clock hitting ". At the same time, there is a constant CLK_TCK, which gives the number of clock hits per second of the machine clock. So to get the running time of a function f, we only need to call clock () before calling f to obtain a clock to hit point C1; then call clock () After f is executed (), get another clock hit point C2; the difference between the two clock hit points (C2-C1) is the number of clock hits consumed by f operation, then divided by the constant CLK_TCK, the running time in seconds is obtained.
Assume that the constant CLK_TCK is 100. Given the number of clock hits obtained twice before and after the tested function, please give the running time of the tested function.
Input:
Enter two integers C1 and C1 in the order of one row. Note that the number of clock hits obtained twice is definitely not the same, that is, C1 <C2, and the value is [0,107].
Output:
Output the running time of the tested function in one row. The running time must be output in the format of "hh: mm: ss" (that is, 2-bit "hour: minute: Second"). The time in less than 1 second is rounded to second.
Sample Input:
123 4577973
Sample Output:
12:42:59
1 #include <cstdio> 2 3 int main() 4 { 5 //freopen("E:\\Temp\\input.txt", "r", stdin); 6 7 int C1, C2; 8 scanf("%d %d", &C1, &C2); 9 10 int ans = C2 - C1;11 if(ans%100 < 50) {12 ans /= 100;13 } else {14 ans = ans/100+1;15 }16 17 printf("%02d:%02d:%02d\n", ans/3600, ans%3600/60, ans%60);18 19 return 0;20 }
B1008. right shift of array element loop (20)
Description:
An array A contains N (N> 0) integers. If another array is not allowed, the position of each integer is shifted to M (M> = 0, that is ...... AN-1) transform to (AN-M ...... AN-1 A0 A1 ...... AN-M-1) (the last M number cycle moves to the M position at the beginning ). If you need to consider how to design a mobile method to minimize the number of APP data moves?
Input:
Each input contains a test case. Input N (1 <= N <= 1st), M (M> = 0), and input N integers in row 100, are separated by spaces.
Output:
Output the integer sequence after the right shift of M digits in a row, which is separated by spaces. No extra spaces are allowed at the end of the sequence.
Sample Input:
6 2
1 2 3 4 5 6
Sample Output:
5 6 1 2 3 4
1 #include <cstdio> 2 3 #define MaxSize 110 4 5 int List[MaxSize]; 6 7 int main() 8 { 9 //freopen("E:\\Temp\\input.txt", "r", stdin);10 11 int N, M, counter = 1;12 scanf("%d %d", &N, &M);13 for(int i=0; i<N; ++i) {14 scanf("%d", &List[i]);15 }16 17 M %= N;18 for(int i=N-M; i<N; ++i) {19 if(counter != N) {20 printf("%d ", List[i]);21 } else {22 printf("%d\n", List[i]);23 }24 counter++;25 }26 for(int i=0; i<N-M; ++i) {27 if(counter != N) {28 printf("%d ", List[i]);29 } else {30 printf("%d\n", List[i]);31 }32 counter++;33 }34 35 return 0;36 }
B1012. digital classification (20)
Description:
Set a series of positive integers, classify the numbers as required, and output the following five numbers:
- A1 = the sum of all the even numbers that can be divisible by 5;
- A2 = will be divided by 5 after the remaining 1 number in the given order of staggered summation, that is, the calculation of n1-n2 + n3-n4 ...;
- A3 = the number of digits after the division of 5;
- A4 = the average of the number 3 after division by 5, accurate to the first digit after the decimal point;
- A5 = the maximum number among the four digits after being divided by five.
Input:
Each input contains one test case. Each test case first gives a positive integer N of no more than 1000, and then gives N Positive Integers of no more than 1000 to be classified. Numbers are separated by spaces.
Output:
Calculate the given N positive integers according to the requirements of the questions ~ A5. Numbers are separated by spaces, but no extra space is allowed at the end of the line.
If a certain type of number does not exist, "N" is output at the corresponding position ".
Sample Input1:
13 1 2 3 4 5 6 7 8 9 10 20 16 18
Sample Output1:
30 11 2 9.7 9
Sample Input2:
8 1 2 4 5 6 7 9 16
Sample Output2:
N 11 2 N 9
1 #include <cstdio> 2 3 #define MaxSize 5 4 5 int List[MaxSize], ans[MaxSize]; 6 7 int main() 8 { 9 //freopen("E:\\Temp\\input.txt", "r", stdin);10 11 int N, temp;12 scanf("%d", &N);13 for(int i=0; i<N; ++i) {14 scanf("%d", &temp);15 if(temp%5 == 0) {16 if(temp%2 == 0) {17 ans[0] += temp;18 List[0]++;19 }20 } else if(temp%5 == 1) {21 if(List[1]%2 == 0) {22 ans[1] += temp;23 } else {24 ans[1] -= temp;25 }26 List[1]++;27 } else if(temp%5 == 2) {28 List[2]++;29 } else if(temp%5 == 3) {30 ans[3] += temp;31 List[3]++;32 } else {33 if(temp > ans[4]) {34 ans[4] = temp;35 }36 List[4]++;37 }38 }39 40 if(List[0] == 0) {41 printf("N ");42 } else {43 printf("%d ", ans[0]);44 }45 if(List[1] == 0) {46 printf("N ");47 } else {48 printf("%d ", ans[1]);49 }50 if(List[2] == 0) {51 printf("N ");52 } else {53 printf("%d ", List[2]);54 }55 if(List[3] == 0) {56 printf("N ");57 } else {58 printf("%.1f ", (double)ans[3]/List[3]);59 }60 if(List[4] == 0) {61 printf("N\n");62 } else {63 printf("%d\n", ans[4]);64 }65 66 return 0;67 }