水题~~
字符串的处理+排序
View Code
1 #include<stdio.h> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int maxn = 5005; 6 struct node{ 7 char name[ 105 ]; 8 int win,lost,diff,score; 9 }team[ maxn ]; 10 int cnt; 11 void init( int n ){ 12 for( int i=0;i<=n;i++ ){ 13 team[ i ].win=team[ i ].lost=team[ i ].diff=team[ i ].score=0; 14 //memset( team[i].name,'\0',sizeof( team[i].name )); 15 } 16 cnt=0; 17 } 18 int find( char tt[] ){ 19 if( cnt==0 ){ 20 strcpy( team[0].name,tt ); 21 cnt++; 22 return 0; 23 } 24 int i; 25 for( i=0;i<cnt;i++ ){ 26 if( strcmp( tt,team[i].name )==0 ){ 27 return i; 28 } 29 } 30 if( i==cnt ){ 31 strcpy( team[cnt].name,tt ); 32 cnt++; 33 return cnt-1; 34 } 35 } 36 bool cmp( node a,node b ){ 37 if( a.score!=b.score ) 38 return a.score>b.score; 39 else if( a.diff!=b.diff ) 40 return a.diff>b.diff; 41 else if( a.win!=b.win ) 42 return a.win>b.win; 43 else if( strcmp( a.name,b.name )>0 ) 44 return false; 45 else return true; 46 } 47 int main(){ 48 int n; 49 while( scanf("%d",&n)!=EOF ){ 50 init( n ); 51 char n1[ 105 ],n2[ 105 ]; 52 int num1,num2; 53 for( int i=0;i<(n*(n-1));i++ ){ 54 scanf("%s VS %s %d:%d",n1,n2,&num1,&num2); 55 int f1=find( n1 ); 56 int f2=find( n2 ); 57 if( num1==num2 ){ 58 team[ f1 ].score++; 59 team[ f2 ].score++; 60 team[ f1 ].win+=num1; 61 team[ f1 ].lost+=num1; 62 team[ f2 ].win+=num2; 63 team[ f2 ].lost+=num2; 64 } 65 else if( num1>num2 ){ 66 team[ f1 ].win+=num1; 67 team[ f1 ].lost+=num2; 68 team[ f2 ].win+=num2; 69 team[ f2 ].lost+=num1; 70 team[ f1 ].score+=3; 71 } 72 else if( num1<num2 ){ 73 team[ f1 ].win+=num1; 74 team[ f1 ].lost+=num2; 75 team[ f2 ].win+=num2; 76 team[ f2 ].lost+=num1; 77 team[ f2 ].score+=3; 78 } 79 } 80 for( int i=0;i<n;i++ ){ 81 team[ i ].diff=team[ i ].win-team[ i ].lost; 82 } 83 sort( team,team+n,cmp ); 84 for( int i=0;i<n;i++ ){ 85 printf("%s %d\n",team[i].name,team[i].score); 86 } 87 printf("\n"); 88 } 89 return 0; 90 }