ZZU省赛 第一题 奇怪排序
一开始拿到这道题时,有点纠结排序后的字符串不知道怎么转回来,最后经别人提醒我把字符和数绑定成一个数组就可以了
View Code
1 #include<stdio.h> 2 #include<stdlib.h> 3 #include<string.h> 4 #include<math.h> 5 6 int i; 7 typedef struct 8 { 9 char s[10]; 10 int n; 11 }numble; 12 numble num[51]; 13 14 char word[51][10], sort[51]; 15 void reverse(int x) 16 { 17 int t=0; 18 char c[10]; 19 while( x != 0) 20 { 21 c[t] = x%10 + '0'; 22 x = x/10; 23 t++; 24 } 25 c[t] = '\0'; 26 strcpy(word[i++],c); 27 } 28 29 int cmp_string(const void *_a,const void *_b) 30 { 31 char* a = (char*) _a; 32 char* b = (char*) _b; 33 if(strlen(a) != strlen(b)) 34 return strlen(a) - strlen(b); 35 else 36 return strcmp(a,b); 37 } 38 39 int cmp(const void *a,const void *b) 40 { 41 return (*(numble *)a).n > (*(numble *)b).n ? 1 : -1; 42 } 43 44 int main() 45 { 46 int j, k, A, B, ncases; 47 int len,n,sum,m; 48 49 scanf("%d",&ncases); 50 while( ncases-- ) 51 { 52 scanf("%d%d",&A,&B); 53 i = 0; 54 for(j=A; j<=B; j++) 55 { 56 reverse(j); 57 } 58 n = B-A+1; 59 qsort(word,n,sizeof(word[0]),cmp_string); 60 for(j=0; j<n; j++) 61 { 62 m = 0; 63 strcpy(num[j].s,word[j]); 64 for(k=0; k<strlen(word[j]); k++) 65 { 66 m = m*10 + word[j][k]-'0'; 67 } 68 num[j].n = m; 69 } 70 qsort(num,n,sizeof(numble),cmp); 71 for(k=0; k<n; k++) 72 { 73 len = strlen(num[k].s); 74 sum = 0; 75 for(j=len-1; j>=0; j--) 76 { 77 sum = sum*10 + num[k].s[j]-'0'; 78 } 79 printf("%d",sum); 80 if(k != n) 81 printf(" "); 82 } 83 printf("\n"); 84 } 85 return 0; 86 }