目录
1092 To Buy or Not to Buy (20 分)
1083 List Grades (25 分)
Given a list of N student records with name, ID and grade. You are supposed to sort the records with respect to the grade in non-increasing order, and output those student records of which the grades are in a given interval.
Input Specification:
Each input file contains one test case. Each case is given in the following format:
N
name[1] ID[1] grade[1]
name[2] ID[2] grade[2]
... ...
name[N] ID[N] grade[N]
grade1 grade2
where name[i]
and ID[i]
are strings of no more than 10 characters with no space, grade[i]
is an integer in [0, 100], grade1
and grade2
are the boundaries of the grade's interval. It is guaranteed that all the grades are distinct.
Output Specification:
For each test case you should output the student records of which the grades are in the given interval [grade1
, grade2
] and are in non-increasing order. Each student record occupies a line with the student's name and ID, separated by one space. If there is no student's grade in that interval, output NONE
instead.
Sample Input 1:
4
Tom CS000001 59
Joe Math990112 89
Mike CS991301 100
Mary EE990830 95
60 100
Sample Output 1:
Mike CS991301
Mary EE990830
Joe Math990112
Sample Input 2:
2
Jean AA980920 60
Ann CS01 80
90 95
Sample Output 2:
NONE
答案:
#include <stdio.h>
#include<stdlib.h>
//结构体
struct student{
char name[20];
char id[20];
int grade;
};
int main()
{
int N, g1, g2;
scanf("%d", &N);
//定义一个结构体
struct student stu[100000];
int i,j;
for (i = 0; i < N; i++)
scanf("%s%s%d", &stu[i].name, &stu[i].id, &stu[i].grade);
scanf("%d%d", &g1, &g2);
//选择排序
//定义temp为student结构体
struct student temp;
for(i=0;i<N-1;i++){
for(j=i+1;j<N;j++){
if(stu[i].grade < stu[j].grade){
temp = stu[i];
stu[i] = stu[j];
stu[j] = temp;
}
}
}
int flag = 0;
for (i = 0; i < N; i++) {
if (stu[i].grade >= g1 && stu[i].grade <= g2) {
printf("%s %s\n", stu[i].name, stu[i].id);
flag = 1;
}
}
if (flag == 0)
printf("NONE");
}
一开始是部分正确,后来发现是在选择排序时只交换了grade,没有交换name和id。
解决:将temp定义为sudent结构体。
1092 To Buy or Not to Buy (20 分)
Eva would like to make a string of beads with her favorite colors so she went to a small shop to buy some beads. There were many colorful strings of beads. However the owner of the shop would only sell the strings in whole pieces. Hence Eva must check whether a string in the shop contains all the beads she needs. She now comes to you for help: if the answer is Yes
, please tell her the number of extra beads she has to buy; or if the answer is No
, please tell her the number of beads missing from the string.
For the sake of simplicity, let's use the characters in the ranges [0-9], [a-z], and [A-Z] to represent the colors. For example, the 3rd string in Figure 1 is the one that Eva would like to make. Then the 1st string is okay since it contains all the necessary beads with 8 extra ones; yet the 2nd one is not since there is no black bead and one less red bead.
Figure 1
Input Specification:
Each input file contains one test case. Each case gives in two lines the strings of no more than 1000 beads which belong to the shop owner and Eva, respectively.
Output Specification:
For each test case, print your answer in one line. If the answer is Yes
, then also output the number of extra beads Eva has to buy; or if the answer is No
, then also output the number of beads missing from the string. There must be exactly 1 space between the answer and the number.
Sample Input 1:
ppRYYGrrYBR2258
YrR8RrY
Sample Output 1:
Yes 8
Sample Input 2:
ppRYYGrrYB225
YrR8RrY
Sample Output 2:
No 2
答案:
#include <stdio.h>
int main(void)
{
int beads[128] = {0};
char ch;
int i, more = 0, less = 0;
//只要输入不是回车,就一直输入字符
while ((ch = getchar()) != '\n') {
//强制转换
beads[(int)ch]++;
}
while ((ch = getchar()) != '\n') {
beads[(int)ch]--;
}
for (i = 0; i < 128; i++) {
if (beads[i] > 0) {
more += beads[i];
} else if (beads[i] < 0) {
less -= beads[i];
}
}
if (less) {
printf("No %d\n", less);
} else {
printf("Yes %d\n", more);
}
return 0;
}
1036 Boys vs Girls (25 分)
This time you are asked to tell the difference between the lowest grade of all the male students and the highest grade of all the female students.
Input Specification:
Each input file contains one test case. Each case contains a positive integer N, followed by N lines of student information. Each line contains a student's name
, gender
, ID
and grade
, separated by a space, where name
and ID
are strings of no more than 10 characters with no space, gender
is either F
(female) or M
(male), and grade
is an integer between 0 and 100. It is guaranteed that all the grades are distinct.
Output Specification:
For each test case, output in 3 lines. The first line gives the name and ID of the female student with the highest grade, and the second line gives that of the male student with the lowest grade. The third line gives the difference gradeF−gradeM. If one such kind of student is missing, output Absent
in the corresponding line, and output NA
in the third line instead.
Sample Input 1:
3
Joe M Math990112 89
Mike M CS991301 100
Mary F EE990830 95
Sample Output 1:
Mary EE990830
Joe Math990112
6
Sample Input 2:
1
Jean M AA980920 60
Sample Output 2:
Absent
Jean AA980920
NA
答案:
#include <stdio.h>
struct student{
char name[11];
char id[11];
char gender;
int grade;
};
int main(void)
{
int a;
scanf("%d",&a);
struct student stu[a];
int i;
int M=-1,F=-1;
for(i=0;i<a;i++){
scanf("%s %s %s %d",&stu[i].name,&stu[i].gender,&stu[i].id,&stu[i].grade);
//字符用单引号''
if(stu[i].gender == 'F'){
if(F==-1 || stu[F].grade<stu[i].grade){
F = i;
}
}else if(stu[i].gender == 'M'){
if(M==-1 || stu[M].grade>stu[i].grade){
M = i;
}
}
}
if(F==-1)
printf("Absent");
else
printf("%s %s",stu[F].name,stu[F].id);
printf("\n");
if(M==-1)
printf("Absent");
else
printf("%s %s",stu[M].name,stu[M].id);
printf("\n");
if(F==-1 || M==-1)
printf("NA");
else
printf("%d",stu[F].grade-stu[M].grade);
}
看注释