First of all, let's talk about the last test point. It should be noted that the final score is a rounded integer, that is to say, the scores of 98.6 and 98.7 are the same. It's really disgusting and made me work for a long time.
1. All data can be int, and the rounding can be increased by 0.5 and then forcibly converted to int
2. Since the student number is not a pure number and cannot be stored in array, qsort and bsearch are required.
3. The last test point has the same situation as 98.6 and 98.7, that is, 59.8 is also qualified. Therefore, it should be rounded when saving. Do not convert only when outputting, and the sorting will be wrong.
4. The priority of ranking method is: qualified first, the final score decreases and the student number increases
5. The online programming score does not exist, and the data will not be stored directly, otherwise it will time out in the end.
subject
For MOOC in Chinese Universities( http://www.icourse163.org/ )Students studying the "data structure" course who want to obtain a qualification certificate must first obtain an online programming homework score of no less than 200 points, and then obtain a total score of no less than 60 points (out of 100). The calculation formula of the total score is G=(Gmid − term) × 40%+Gfinal × 60%), if − Gmid − term > gfinal; Otherwise, the general evaluation , G , is , gfinal. Here − Gmid − term and − gfinal are the students' midterm and final grades respectively.
The problem now is that each exam produces an independent report card. Please write a program to combine different transcripts into one.
Input format:
Input: three integers are given in the first line, which are P (the number of students who have done online programming homework), M (the number of students who have taken the midterm exam) and N (the number of students who have taken the final exam). Each number does not exceed 10000.
Next, there are three inputs. The first block contains P online programming scores {Gp; The second block contains M mid-term test scores − Gmid − term; The third block contains N final exam scores {Gfinal. Each grade occupies one line in the form of student number score. The student number shall be English letters and numbers with no more than 20 characters; The score is a non negative integer (the maximum total score for programming is 900, and the maximum score at the end of the period is 100).
Output format:
Print out the list of students who have obtained the qualification certificate. One line for each student in the format:
Student ID − Gp − Gmid − term − Gfinal − G
If some scores do not exist (for example, someone did not take the mid-term exam), output "− 1" in the corresponding position. The output order is decreasing according to the total evaluation score (rounded to the nearest integer). If there is juxtaposition, it will be increased by student number. Ensure that the student number is not repeated and there is at least one qualified student.
6 6 7
01234 880
a1903 199
ydjh2 200
wehu8 300
dx86w 220
missing 400
ydhfu77 99
wehu8 55
ydjh2 98
dx86w 88
a1903 86
01234 39
ydhfu77 88
a1903 66
01234 58
wehu8 84
ydjh2 82
missing 99
dx86w 81
Output example:
missing 400 -1 99 99
ydjh2 200 98 82 88
dx86w 220 88 81 84
wehu8 300 55 84 84
#include<stdio.h> #include<string.h> #include<stdlib.h> typedef struct student{ char name[25]; /*Storage student number*/ int p; /*Save online programming scores*/ int m; /*Save midterm exam results*/ int n; /*Save final grade*/ int zong; /*End of storage period*/ int check; /*Is it qualified*/ }stu; int comp1(const void* a,const void* b); /*In ascending order of student number*/ int comp2(const void* a,const void* b); /*Sort total function*/ int comp3(const void* a,const void* b); /*Find function by student number*/ int main(){ int p,m,n,i=0,x=0; /*Store all kinds of test results*/ int mp,mm,mn; char name[25]; stu a[100000]; /*Here are 100000 students*/ stu *dent; scanf("%d %d %d",&p,&m,&n); while(p--) {scanf("%s %d",name,&mp); getchar(); strcpy(a[i].name,name); a[i].p=mp; a[i].m=-1; /*The other two scores are initialized, and there is no default*/ a[i].n=-1; i++; } qsort(a,i,sizeof(stu),comp1); while(m--) {scanf("%s %d",name,&mm); getchar(); dent=(stu*)bsearch(name,a,i,sizeof(stu),comp3); if(dent!=0) /*If there is no result in online programming, it must be unqualified and will not be considered*/ dent->m=mm; } while(n--) {scanf("%s %d",name,&mn); getchar(); dent=(stu*)bsearch(name,a,i,sizeof(stu),comp1); if(dent!=0) dent->n=mn; } for(x=0;x<i;x++) /*This cycle calculates the final score and determines whether it is qualified*/ {if(a[x].m<a[x].n) {a[x].zong=a[x].n; if(a[x].zong>=60&&a[x].p>=200) {a[x].check=1; } else a[x].check=0; continue; } a[x].zong=(int)(a[x].m*0.4+a[x].n*0.6+0.5); /*Store after rounding*/ if(a[x].zong+0.5>=60&&a[x].p>=200) {a[x].check=1; } else a[x].check=0; } qsort(a,i,sizeof(stu),comp2); /*sort*/ for(x=0;x<i&&a[x].check==1;x++) /*output*/ printf("%s %d %d %d %d\n",a[x].name,a[x].p,a[x].m,a[x].n,a[x].zong); } int comp1(const void* a,const void* b) {stu* x=(stu*)a; stu* y=(stu*)b; return strcmp(x->name,y->name); /*Ascending by name*/ } int comp2(const void* a,const void* b) {stu* x=(stu*)a; stu* y=(stu*)b; if(x->check != y->check) /*Qualified first*/ return y->check - x->check; else if(x->zong != y->zong) /*Those with high final scores are in the front*/ return y->zong - x->zong; else /*Ascending by name*/ return strcmp(x->name,y->name); } int comp3(const void* a,const void* b) {char *x=(char*)a; stu *y=(stu*)b; return strcmp(x,y->name); /*lookup*/ }
The points needing attention are written in the beginning and comments. I changed the method before, but the running time is half the weight. This code is relatively concise.