A problem of sorting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1044 Accepted Submission(s): 438
Problem Description
There are many people's name and birth in a list.Your task is to print the name from young to old.(There is no pair of two has the same age.)
Input
First line contains a single integer
T≤100
which denotes the number of test cases.
For each test case, there is an positive integer n(1≤n≤100) which denotes the number of people,and next n lines,each line has a name and a birth's year(1900-2015) separated by one space.
The length of name is positive and not larger than 100 .Notice name only contain letter(s),digit(s) and space(s).
For each test case, there is an positive integer n(1≤n≤100) which denotes the number of people,and next n lines,each line has a name and a birth's year(1900-2015) separated by one space.
The length of name is positive and not larger than 100 .Notice name only contain letter(s),digit(s) and space(s).
Output
For each case, output
n
lines.
Sample Input
2 1 FancyCoder 1996 2 FancyCoder 1996 xyz111 1997
Sample Output
FancyCoder xyz111 FancyCoder
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 101
int year[maxn],num[maxn];
char name[maxn][107];
int comp(int a,int b){
return year[a] > year[b];
};
int main(){
int t, n;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
getchar();
for(int i= 0;i <n ; i++){
num[i] = i;
int len = 0;
while(1){
name[i][len] = getchar();
if(name[i][len] == '\n') break;
len++;
}
name[i][len-5]= '\0';
year[i] = 0;
for(int j = len-4;j < len; j++)
year[i] = year[i]*10+name[i][j]-'0';
}
sort(num,num+n,comp);
for(int i = 0;i < n; i++){
printf("%s\n",name[num[i]]);
}
}
return 0;
}
本文介绍了一个简单的年龄排序问题,任务是根据输入的人名和出生年份列表,将人名按年龄从小到大的顺序输出。文章提供了完整的代码实现,包括读取输入、解析姓名和出生年份、比较和排序算法。
1055

被折叠的 条评论
为什么被折叠?



