PAT/简单模拟习题集(一)

B1001.害死人不偿命的(3n+1)猜想 (15)

Description:

卡拉兹(Callatz)猜想:

对任何一个自然数n,如果它是偶数,那么把它砍掉一半;如果它是奇数,那么把(3n+1)砍掉一半。这样一直反复砍下去,最后一定在某一步得到n=1。卡拉兹在1950年的世界数学家大会上公布了这个猜想,传说当时耶鲁大学师生齐动员,拼命想证明这个貌似很傻很天真的命题,结果闹得学生们无心学业,一心只证(3n+1),以至于有人说这是一个阴谋,卡拉兹是在蓄意延缓美国数学界教学与科研的进展……

我们今天的题目不是证明卡拉兹猜想,而是对给定的任一不超过1000的正整数n,简单地数一下,需要多少步(砍几下)才能得到n=1?

Input:

每个测试输入包含1个测试用例,即给出自然数n的值。

Output:

输出从n计算到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
", step);
20 
21     return 0;
22 }

B1032. 挖掘机技术哪家强 (20)

Description:

为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛。现请你根据比赛结果统计出技术最强的那个学校。

Input:

输入在第1行给出不超过105的正整数N,即参赛人数。随后N行,每行给出一位参赛者的信息和成绩,包括其所代表的学校的编号(从1开始连续编号)、及其比赛成绩(百分制),中间以空格分隔。

Output:

在一行中给出总得分最高的学校的编号、及其总分,中间以空格分隔。题目保证答案唯一,没有并列。

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
", ID, maxscore);
27 }

B1011. A+B和C (15)

Description:

给定区间[-231, 231]内的3个整数A、B和C,请判断A+B是否大于C。

Input:

输入第1行给出正整数T(<=10),是测试用例的个数。随后给出T组测试用例,每组占一行,顺序给出A、B和C。整数间以空格分隔。

Output:

对每组测试用例,在一行中输出“Case #X: true”如果A+B>C,否则输出“Case #X: false”,其中X是测试用例的编号(从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
", i);
14         } else {
15             printf("Case #%d: false
", i);
16         }
17     }
18 
19     return 0;
20 }

B1016. 部分A+B (15)

Description:

正整数A的“DA(为1位整数)部分”定义为由A中所有DA组成的新整数PA。例如:给定A = 3862767,DA = 6,则A的“6部分”PA是66,因为A中有2个6。

现给定A、DA、B、DB,请编写程序计算PA + PB

Input:

输入在一行中依次给出A、DA、B、DB,中间以空格分隔,其中0 < A, B < 1010

Output:

在一行中输出PA + PB的值。

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
", pA+pB);
25 
26     return 0;
27 }

B1026. 程序运行时间 (15)

Description:

要获得一个C语言程序的运行时间,常用的方法是调用头文件time.h,其中提供了clock()函数,可以捕捉从程序开始运行到clock()被调用时所耗费的时间。这个时间单位是clock tick,即“时钟打点”。同时还有一个常数CLK_TCK,给出了机器时钟每秒所走的时钟打点数。于是为了获得一个函数f的运行时间,我们只要在调用f之前先调用clock(),获得一个时钟打点数C1;在f执行完成后再调用clock(),获得另一个时钟打点数C2;两次获得的时钟打点数之差(C2-C1)就是f运行所消耗的时钟打点数,再除以常数CLK_TCK,就得到了以秒为单位的运行时间。

这里不妨简单假设常数CLK_TCK为100。现给定被测函数前后两次获得的时钟打点数,请你给出被测函数运行的时间。

Input:

输入在一行中顺序给出2个整数C1和C1。注意两次获得的时钟打点数肯定不相同,即C1 < C2,并且取值在[0, 107]。

Output:

在一行中输出被测函数运行的时间。运行时间必须按照“hh:mm:ss”(即2位的“时:分:秒”)格式输出;不足1秒的时间四舍五入到秒。

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
", ans/3600, ans%3600/60, ans%60);
18 
19     return 0;
20 }

B1008. 数组元素循环右移问题 (20)

Description:

一个数组A中存有N(N>0)个整数,在不允许使用另外数组的前提下,将每个整数循环向右移M(M>=0)个位置,即将A中的数据由(A0A1……AN-1)变换为(AN-M …… AN-1 A0 A1……AN-M-1)(最后M个数循环移至最前面的M个位置)。如果需要考虑程序移动数据的次数尽量少,要如何设计移动的方法?

Input:

每个输入包含一个测试用例,第1行输入N ( 1<=N<=100)、M(M>=0);第2行输入N个整数,之间用空格分隔。

Output:

在一行中输出循环右移M位以后的整数序列,之间用空格分隔,序列结尾不能有多余空格。

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
", 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
", List[i]);
31         }
32         counter++;
33     }
34 
35     return 0;
36 }

B1012. 数字分类 (20)

Description:

定一系列正整数,请按要求对数字进行分类,并输出以下5个数字:

  • A1 = 能被5整除的数字中所有偶数的和;
  • A2 = 将被5除后余1的数字按给出顺序进行交错求和,即计算n1-n2+n3-n4...;
  • A3 = 被5除后余2的数字的个数;
  • A4 = 被5除后余3的数字的平均数,精确到小数点后1位;
  • A5 = 被5除后余4的数字中最大数字。

Input:

每个输入包含1个测试用例。每个测试用例先给出一个不超过1000的正整数N,随后给出N个不超过1000的待分类的正整数。数字间以空格分隔。

Output:

对给定的N个正整数,按题目要求计算A1~A5并在一行中顺序输出。数字间以空格分隔,但行末不得有多余空格。

若其中某一类数字不存在,则在相应位置输出“N”。

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
");
62     } else {
63         printf("%d
", ans[4]);
64     }
65 
66     return 0;
67 }
原文地址:https://www.cnblogs.com/VincentValentine/p/6028798.html