PTA_通讯录排序_JAVA

通讯录排序


输入n个朋友的信息,包括姓名、生日、电话号码,本题要求编写程序,按照年龄从大到小的顺序依次输出通讯录。题目保证所有人的生日均不相同。

输入格式:

输入第一行给出正整数n(<10)。随后n行,每行按照“姓名 生日 电话号码”的格式给出一位朋友的信息,其中“姓名”是长度不超过10的英文字母组成的字符串,“生日”是yyyymmdd格式的日期,“电话号码”是不超过17位的数字及+-组成的字符串。

输出格式:

按照年龄从大到小输出朋友的信息,格式同输出。

输入样例:

3
zhang 19850403 13912345678
wang 19821020 +86-0571-88018448
qian 19840619 13609876543

输出样例:

wang 19821020 +86-0571-88018448
qian 19840619 13609876543
zhang 19850403 13912345678


import java.util.*;
public class Main {
	private static final boolean True = false;
	public static void main(String[] args) {
		int n , i ,j;
		people temp = new people();
		Scanner input = new Scanner (System.in);
		Main work = new Main();
		n = input.nextInt();
		//建立并初始化people数组
		people[] list =new people[n];
		for(i = 0;i<n;i++)
		{
			list[i] = new people();
		}
		//输入n个朋友的信息
		for(i = 0;i<n;i++)
		{
			list[i].name = input.next();
			list[i].birthday = input.next();
			list[i].phone_number = input.next();
		}
		//对n个朋友的信息进行排序(调用compare方法)
		for(i = 0;i<n-1;i++)
		{
			for(j = n-1;j>i;j--)
			{
				if(work.compare(list[i],list[j]))
				{
					temp = list[i];
					list[i] = list[j];
					list[j] = temp;
				}
			}
		}
		for(i = 0;i<n;i++)
		{
			work.Show(list[i]);
		}
		input.close();
		
	}
	//compare方法用于判断生日大小,如若a大于b则返回True
	public boolean compare(people a,people b)
	{
		long birthday_a,birthday_b;
		birthday_a = Integer.parseInt(a.birthday);
		birthday_b = Integer.parseInt(b.birthday);
		if(birthday_a>birthday_b)
			return true;
		else
			return false;
	}
	public	void Show (people n)
	{
		System.out.println(n.name+' '+n.birthday+' '+n.phone_number);;
	}

}
class people
{
	String name;
	String birthday;
	String phone_number;
	
	}

猜你喜欢

转载自blog.csdn.net/The_last_word/article/details/89715032