Result: Accepted
Time: 28ms
Memory: 1608kB
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
struct abc{
char num[6];
int g;
int c;
int x;
int sum;
double ave;
};
int cmp(const void *x,const void *y){
if(((struct abc *)x)->sum!=((struct abc *)y)->sum) {
return ((struct abc *)y)->sum - ((struct abc *)x)->sum;
}
if(((struct abc *)x)->g!=((struct abc *)y)->g) {
return ((struct abc *)y)->g - ((struct abc *)x)->g;
}
if(((struct abc *)x)->c!=((struct abc *)y)->c) {
return ((struct abc *)y)->c - ((struct abc *)x)->c;
}
if(((struct abc *)x)->x!=((struct abc *)y)->x) {
return ((struct abc *)y)->x - ((struct abc *)y)->x;
}
else {
return strcmp(((struct abc *)x)->num,((struct abc *)y)->num);
}
}
int main(){
int n,m,cnt,i,j,change,y;
double ave[4];
struct abc o[10001];
char str[6];
scanf("%d",&n);
for(i = 0;i<n;i++){
scanf("%s %d %d %d",&o[i].num,&o[i].g,&o[i].c,&o[i].x);
o[i].sum = o[i].g+o[i].c+o[i].x;
ave[1]+=(double)o[i].g;
ave[2]+=(double)o[i].c;
ave[3]+=(double)o[i].x;
}
ave[1]/=n*1.0;//printf("1:%.4f\n",ave[1]);//ave[1] = (ave[1]*100+0.5)/100;
ave[2]/=n*1.0;//printf("2:%.4f\n",ave[2]);//ave[2] = (ave[2]*100+0.5)/100;
ave[3]/=n*1.0;//printf("3:%.4f\n",ave[3]);//ave[3] = (ave[3]*100+0.5)/100;
scanf("%d",&m);
cnt = 0;
while(m--){
scanf("%d ",&change);
if(change==1){
scanf("%s",str);
cnt++;
for(i = 0;i<n;i++){
if(strcmp(str,o[i].num)==0){
printf("Query #%d: 1 %s\n",cnt,str);
printf("%d %d %d %d\n",o[i].sum,o[i].g,o[i].c,o[i].x);
break;
}
}
}
if(change==2){
scanf("%d",&y);
cnt++;
printf("Query #%d: 2 %d\n",cnt,y);
printf("%.2lf\n",ave[y]);
}
}
qsort(o,n,sizeof(struct abc),cmp);
for(i = 0;i<n;i++){
printf("%s %d %d %d %d\n",o[i].num,o[i].sum,o[i].g,o[i].c,o[i].x);
}
}