直白的排序题 难得的一次AC
1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 typedef struct 5 { 6 char name[10] ; 7 int age; 8 int worth; 9 } people; 10 people p[100000]; 11 int N,K; 12 int M,amin,amax,flag; 13 void search(int m,int min,int max ) 14 { 15 int j,count=0; 16 for(j=0;j<N;j++) 17 { 18 if(p[j].age>=min&&p[j].age<=max) 19 { 20 printf("%s %d %d\n",p[j].name,p[j].age,p[j].worth); 21 count++; 22 flag=1; 23 } 24 if(count==m) 25 break; 26 } 27 } 28 int cmp( const void *a , const void *b) 29 { 30 people *c = (people *)a; 31 people *d = (people *)b; 32 if(d->worth!= c->worth) return (d->worth-c->worth); 33 else if(c->age !=d->age) return (c->age-d->age); 34 else return strcmp((*c).name,(*d).name); 35 } 36 main(){ 37 scanf("%d%d",&N,&K); 38 int i; 39 for(i=0;i<N;i++) 40 { 41 scanf("%s %d %d",p[i].name,&p[i].age,&p[i].worth); 42 } 43 qsort(p,N,sizeof(people),cmp); 44 for(i=1;i<=K;i++) 45 { 46 scanf("%d%d%d",&M,&amin,&amax); 47 printf("Case #%d:\n",i); 48 search(M,amin,amax); 49 if(flag==0) 50 printf("None\n"); 51 flag=0; 52 } 53 }
PAT The World's Richest
时间: 2024-09-29 22:29:43