标签:
http://acm.hdu.edu.cn/showproblem.php?pid=5427
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1447 Accepted Submission(s): 554
被水题pe了一早上,学了个fgets来取代gets,据说后者不太安全,最好不要用。
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 using namespace std; 6 7 const int maxn = 111; 8 9 char name[maxn][maxn]; 10 int age[maxn]; 11 int ran[maxn]; 12 13 int n; 14 15 bool cmp(int x, int y) { 16 return age[x] > age[y]; 17 } 18 19 void init() { 20 21 } 22 23 int main() { 24 int tc; 25 scanf("%d", &tc); 26 while (tc--) { 27 //用scanf("%d\n",&n);代替以下两行会PE。 28 scanf("%d", &n); 29 getchar(); 30 for (int i = 0; i < n; i++) { 31 //fgets得到的字符串,在‘\0‘之前会多一位‘\n‘! 32 fgets(name[i], sizeof(name[i]), stdin); 33 // gets(name[i]); 不安全,不要用 34 int len = strlen(name[i]); 35 age[i] = 0; 36 for (int j = len - 5; j < len-1; j++) { 37 age[i] = age[i] * 10 + name[i][j] - ‘0‘; 38 } 39 name[i][len - 6] = ‘\0‘; 40 } 41 for (int i = 0; i < n; i++) { 42 //printf("str:%s.age:%d\n", name[i],age[i]); 43 } 44 for (int i = 0; i < n; i++) ran[i] = i; 45 sort(ran, ran + n, cmp); 46 for (int i = 0; i < n; i++) { 47 int x = ran[i]; 48 printf("%s\n", name[x]); 49 } 50 } 51 return 0; 52 }
HDU 5427 A problem of sorting 水题
标签:
原文地址:http://www.cnblogs.com/fenice/p/5265300.html