设为首页 加入收藏

TOP

PAT/查找元素习题集(一)
2017-10-12 10:12:40 】 浏览:10150
Tags: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
首页 上一页 1 2 3 4 下一页 尾页 1/4/4
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇C语言-预估校正法求常微分方程 下一篇学习C的笔记

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目