HDOJ 5427 A problem of sorting

本文探讨了如何通过编程解决排序问题,特别是针对一组人员的年龄进行从年轻到年老的排序。通过输入人员数量及每个人的姓名和出生年份,程序能够输出按年龄排序后的人名列表。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A problem of sorting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1215    Accepted Submission(s): 485


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 T100 which denotes the number of test cases.

For each test case, there is an positive integer n(1n100) 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 题意:给出一张许多人的年龄和生日表。你需要从年轻到年老输出人们的名字。(没有人年龄相同) 注意:姓名可以含有空格 ac代码:
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stack>
#include<queue>
#include<iostream>
#include<algorithm>
#define MAXN 10001
#define LL long long
#define INF 0x7fffffff
#define mem(x) memset(x,0,sizeof(x))
using namespace std;
struct s
{
	char name[110];
	int num;
}a[110];
bool cmp(s a,s b)
{
	return a.num>b.num;
}
int main()
{
	int t,i,j;
	int n;
	char ss[1010];
	char str[1010];
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		getchar();
		for(i=0;i<n;i++)
		{
			gets(ss);
			//printf("yes\n");
			int len=strlen(ss);
			int q=0;
			int k=1;
			int w=0;
			int bz=0;
			for(j=len-1;j>=0;j--)
			{
				if(ss[j]==' '&&bz==0)
				{
					bz=1;
					continue;
				}
				if(bz)
				{
					str[w++]=ss[j];
				}
				else
				{
					q=q+(ss[j]-'0')*k;
					k*=10;
				}
			}
			a[i].num=q;
			k=0;
			for(j=w-1;j>=0;j--)
			a[i].name[k++]=str[j];
			a[i].name[k]='\0';
			//getchar();
		}
		sort(a,a+n,cmp);
		for(i=0;i<n;i++)
		printf("%s\n",a[i].name);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值