题目链接:http://vjudge.net/problem/HDU-1236
很疑惑我的代码在OJ过不了TLE,或者Run Time Error
1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <iostream> 5 #include <algorithm> 6 #include <string> 7 #include <cstdlib> 8 9 using namespace std; 10 11 typedef struct 12 { 13 char id[100]; 14 int grade; 15 }people; 16 people x[1005]; 17 18 bool cmp(people a,people b) 19 { 20 if(a.grade==b.grade) 21 return strcmp(a.id,b.id); 22 return a.grade<b.grade; 23 } 24 25 int main() 26 { 27 int n,m,g,i,j,ti[10],num,temp,cnt; 28 while(~scanf("%d",&n)&&n) 29 { 30 cnt=0; 31 memset(ti,0,sizeof(ti)); 32 memset(x,0,sizeof(x)); 33 scanf("%d %d",&m,&g); 34 35 for(i=1;i<=m;i++) 36 scanf("%d",&ti[i]); 37 38 for(i=1;i<=n;i++) 39 { 40 scanf("%s",x[i].id); 41 scanf("%d",&num); 42 x[i].grade=0; 43 for(j=1;j<=num;j++) 44 { 45 scanf("%d",&temp); 46 x[i].grade+=ti[temp]; 47 } 48 if(x[i].grade>=g) cnt++; 49 } 50 51 sort(x+1,x+1+n,cmp); 52 if (cnt==0) printf("0 "); 53 else 54 { 55 printf("%d ",cnt); 56 for(i=1;i<=cnt;i++) 57 printf("%s %d ",x[i].id,x[i].grade); 58 } 59 } 60 return 0; 61 }
本地测试毫无问题...某人用qsort过了我用sort就出各种问题...(下面是wubaizhe AC的代码,我的还需要改)
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 #include<ctype.h> 6 #include<float.h> 7 #define maxn 1005 8 typedef struct{ 9 char num[25]; 10 int grade; 11 }student; 12 student b[maxn]; 13 int a[20]; 14 int comp(const void* a,const void* b){ 15 student *p1 = (student*)a; 16 student *p2 = (student*)b; 17 if(p2->grade == p1->grade) //若分数相同,则用strcmp函数比较(很简单的道理,仔细想想就能相通) 18 return strcmp(p1->num,p2->num); 19 return (p2->grade - p1->grade); 20 } 21 int main(){ 22 int n,m,g,i,j,N,tinum,cnt; 23 while(scanf("%d",&n) != EOF && n){ 24 cnt = 0; 25 memset(a,0,sizeof(a)); 26 memset(b,0,sizeof(b)); 27 scanf("%d%d",&m,&g); 28 for(i=0 ;i<m ;i++) 29 scanf("%d",&a[i]); 30 for(i=0 ;i<n ;i++){ 31 scanf("%s",b[i].num); 32 scanf("%d",&N); 33 for(j=0 ;j<N ;j++){ 34 scanf("%d",&tinum); 35 b[i].grade += a[tinum - 1]; 36 } 37 if(b[i].grade >= g) 38 cnt++; 39 } 40 qsort(b,n,sizeof(b[0]),comp); 41 if(cnt == 0){ 42 printf("0 "); 43 } 44 else{ 45 printf("%d ",cnt); 46 for(i=0 ;i<cnt ;i++){ 47 printf("%s %d ",b[i].num,b[i].grade); 48 } 49 } 50 } 51 return 0; 52 }