程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> 關於C語言 >> PAT/查找元素習題集,pat元素習題集

PAT/查找元素習題集,pat元素習題集

編輯:關於C語言

PAT/查找元素習題集,pat元素習題集


B1004. 成績排名 (20)

Description:

讀入n名學生的姓名、學號、成績,分別輸出成績最高和成績最低學生的姓名和學號。

Input:

每個測試輸入包含1個測試用例,格式為:

第1行:正整數n
  第2行:第1個學生的姓名 學號 成績
  第3行:第2個學生的姓名 學號 成績
  ... ... ...
  第n+1行:第n個學生的姓名 學號 成績

其中姓名和學號均為不超過10個字符的字符串,成績為0到100之間的一個整數,這裡保證在一組測試用例中沒有兩個學生的成績是相同的。

Output:

對每個測試用例輸出2行,第1行是成績最高學生的姓名和學號,第2行是成績最低學生的姓名和學號,字符串間有1空格。

Sample Input:

3
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95

Sample Output:

Mike CS991301
Joe Math990112

 1 #include <cstdio>
 2 
 3 struct Student{
 4     char name[15];
 5     char id[15];
 6     int score;
 7 }temp, ans_max, ans_min;
 8 
 9 int main()
10 {
11     int n;
12     scanf("%d", &n);
13     ans_max.score = -1;
14     ans_min.score = 101;
15     for(int i=0; i<n; ++i) {
16         scanf("%s%s%d", temp.name, temp.id, &temp.score);
17         if(temp.score > ans_max.score)  ans_max = temp;
18         if(temp.score < ans_min.score)  ans_min = temp;
19     }
20 
21     printf("%s %s\n%s %s\n", ans_max.name, ans_max.id, ans_min.name, ans_min.id);
22 
23     return 0;
24 }

 

B1028. 人口普查 (20)

Description:

某城鎮進行人口普查,得到了全體居民的生日。現請你寫個程序,找出鎮上最年長和最年輕的人。

這裡確保每個輸入的日期都是合法的,但不一定是合理的——假設已知鎮上沒有超過200歲的老人,而今天是2014年9月6日,所以超過200歲的生日和未出生的生日都是不合理的,應該被過濾掉。

Input:

輸入在第一行給出正整數N,取值在(0, 105];隨後N行,每行給出1個人的姓名(由不超過5個英文字母組成的字符串)、以及按“yyyy/mm/dd”(即年/月/日)格式給出的生日。題目保證最年長和最年輕的人沒有並列。

Output:

在一行中順序輸出有效生日的個數、最年長人和最年輕人的姓名,其間以空格分隔。

Sample Input:

5
John 2001/05/12
Tom 1814/09/06
Ann 2121/01/30
James 1814/09/05
Steve 1967/11/20

Sample Output:

3 Tom John

 1 #include <cstdio>
 2 #include <cstring>
 3 
 4 #define MaxSize 11
 5 struct birthday {
 6     char name[MaxSize];
 7     char date[MaxSize];
 8 }temp, maxn, minn;
 9 char floor[MaxSize] = "1814/09/06", upper[MaxSize] = "2014/09/06";
10 
11 int main()
12 {
13     //freopen("E:\\Temp\\input.txt", "r", stdin);
14 
15     for(int i=0; i<MaxSize; ++i) {
16         maxn.date[i] = floor[i];
17         minn.date[i] = upper[i];
18     }
19     int N;
20     scanf("%d", &N);
21     int counter = N;
22     for(int i=0; i<N; ++i) {
23         scanf("%s %s", temp.name, temp.date);
24         if(strcmp(temp.date, floor)<0 || strcmp(temp.date, upper)>0)
25             --counter;
26         else {
27             if(strcmp(temp.date, maxn.date) >= 0)   maxn = temp;
28             if(strcmp(temp.date, minn.date) <= 0)   minn = temp;
29         }
30     }
31 
32     if(counter != 0)
33         printf("%d %s %s\n", counter, minn.name, maxn.name);
34     else
35         printf("0\n");
36 
37     return 0;
38 }
 1 #include <cstdio>
 2 
 3 struct person {
 4     char name[10];
 5     int yy, mm, dd;
 6 }oldest, youngest, left, right, temp;
 7 
 8 bool LessEqu(person a, person b)
 9 {
10     if(a.yy != b. yy)   return a.yy <= b.yy;
11     else if(a.mm != b.mm)   return a.mm <= b.mm;
12     else return a.dd <= b.dd;
13 }
14 bool MoreEqu(person a, person b)
15 {
16     if(a.yy != b. yy)   return a.yy >= b.yy;
17     else if(a.mm != b.mm)   return a.mm >= b.mm;
18     else return a.dd >= b.dd;
19 }
20 void init()
21 {
22     youngest.yy = left.yy = 1814;
23     oldest.yy = right.yy = 2014;
24     youngest.mm = oldest.mm = left.mm = right.mm = 9;
25     youngest.dd = oldest.dd = left.dd = right.dd = 6;
26 }
27 
28 int main()
29 {
30     init();
31     int n, num = 0;
32     scanf("%d", &n);
33     for(int i=0; i<n; ++i) {
34         scanf("%s %d/%d/%d", temp.name, &temp.yy, &temp.mm, &temp.dd);
35         if(MoreEqu(temp, left) && LessEqu(temp, right)) {
36             num++;
37             if(LessEqu(temp, oldest))  oldest = temp;
38             if(MoreEqu(temp, youngest)) youngest = temp;
39         }
40     }
41 
42     if(num == 0)    printf("0\n");
43     else printf("%d %s %s\n", num, oldest.name, youngest.name);
44 
45     return 0;
46 }

 

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 const int maxn = 100010;
 4 int school[maxn];
 5 
 6 int main()
 7 {
 8     int n, schID, score;
 9     scanf("%d", &n);
10     for(int i=0; i<n; ++i) {
11         scanf("%d%d", &schID, &score);
12         school[schID] += score;
13     }
14 
15     int k = 1, MAX = -1;
16     for(int i=1; i<=n; ++i) {
17         if(school[i] > MAX) {
18             MAX = school[i];
19             k = i;
20         }
21     }
22 
23     printf("%d %d\n", k, MAX);
24 
25     return 0;
26 }

 

A1011. World Cup Betting (20)

Description:

With the 2010 FIFA World Cup running, football fans the world over were becoming increasingly excited as the best players from the best teams doing battles for the World Cup trophy in South Africa. Similarly, football betting fans were putting their money where their mouths were, by laying all manner of World Cup bets.

Chinese Football Lottery provided a "Triple Winning" game. The rule of winning was simple: first select any three of the games. Then for each selected game, bet on one of the three possible results -- namely W for win, T for tie, and L for lose. There was an odd assigned to each result. The winner's odd would be the product of the three odds times 65%.

For example, 3 games' odds are given as the following:

 W    T    L
1.1  2.5  1.7
1.2  3.0  1.6
4.1  1.2  1.1

To obtain the maximum profit, one must buy W for the 3rd game, T for the 2nd game, and T for the 1st game. If each bet takes 2 yuans, then the maximum profit would be (4.1*3.0*2.5*65%-1)*2 = 37.98 yuans (accurate up to 2 decimal places).

Input:

Each input file contains one test case. Each case contains the betting information of 3 games. Each game occupies a line with three distinct odds corresponding to W, T and L.

Output:

For each test case, print in one line the best bet of each game, and the maximum profit accurate up to 2 decimal places. The characters and the number must be separated by one space.

Sample Input:

1.1 2.5 1.7
1.2 3.0 1.6
4.1 1.2 1.1

Sample Output:

T T W 37.98

 1 #include <cstdio>
 2 
 3 char S[3] = {'W', 'T', 'L'};
 4 
 5 int main()
 6 {
 7     double ans = 1, temp, a;
 8     int idx;
 9     for(int i=0; i<3; ++i) {
10         temp = 0;
11         for(int j=0; j<3; ++j) {
12             scanf("%lf", &a);
13             if(a > temp) {
14                 temp = a;
15                 idx = j;
16             }
17         }
18         ans *= temp;
19         printf("%c ", S[idx]);
20     }
21 
22     printf("%.2f\n", (ans*0.65-1)*2);
23 
24     return 0;
25 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved