ECNU2939

(先对数字排序,再对字符排序)

View Code
 1 #include<stdio.h>

 2 #include<stdlib.h>

 3 #include<string.h>

 4 struct g{

 5     char name[32];

 6     int sum;

 7 }gl[305]; 

 8 int cmp2( const void *a ,const void *b) 

 9 { 

10     return (*(struct g *)b).sum > (*(struct g *)a).sum ? 1 : -1; 

11 }

12 int main()

13 {

14     int i,t,n,j,r,p,b,g,r0,p0,b0,g0,max;

15     char ch1[32],ch2[32];

16     scanf("%d",&t);

17     getchar();

18     while(t--){

19         r0=0;p0=0;b0=0;g0=0;

20         scanf("%s %d",ch1,&n);

21         getchar();

22         for(i=0;i<strlen(ch1);i++){

23             if(ch1[i]=='R')

24                 r0++;

25             if(ch1[i]=='P')

26                 p0++;

27             if(ch1[i]=='B')

28                 b0++;

29             if(ch1[i]=='G')

30                 g0++;

31         }//WWB的名字

32         for(i=0;i<n;i++){

33             scanf("%s",gl[i].name);

34             r=r0;p=p0;b=b0;g=g0;

35             for(j=0;j<strlen(gl[i].name);j++){

36             if(gl[i].name[j]=='R')

37                 r++;

38             if(gl[i].name[j]=='P')

39                 p++;

40             if(gl[i].name[j]=='B')

41                 b++;

42             if(gl[i].name[j]=='G')

43                 g++;

44             }

45             gl[i].sum=((r*r*r*r+p*p*p+b*b+g)*(r+p*p+b*b*b+g*g*g*g))%1211;

46         }//各个候选人

47         qsort(gl,n,sizeof(gl[0]),cmp2);//对数字排序

48         strcpy(ch2,gl[0].name);

49         max=gl[0].sum;

50         for(j=1;j<n;j++){

51             if(gl[j].sum==max&&(strcmp(gl[j].name,ch2)<0))

52                 strcpy(ch2,gl[j].name);

53         }//对字符排序

54         printf("%s\n",ch2);

55     }

56     return 0;

57 }

你可能感兴趣的:(c)