java P1104 生日

题目描述
cjf君想调查学校OI组每个同学的生日,并按照从大到小的顺序排序。但cjf君最近作业很多,没有时间,所以请你帮她排序。

输入格式
有2行,

第1行为OI组总人数n。

第2行至第n+1行分别是每人的姓名s、出生年y、月m、日d。

输出格式
有n行,

即n个生日从大到小同学的姓名。(如果有两个同学生日相同,输入靠后的同学先输出)

输入输出样例

输入 #1

3
Yangchu 1992 4 23
Qiujingya 1993 10 13
Luowen 1991 8 1

输出 #1

Luowen
Yangchu
Qiujingya

扫描二维码关注公众号,回复: 10248445 查看本文章
import java.util.*;

public class Main {

	public static void main(String[] args) {
		Main m = new Main();
		m.handleInput();
	}

	int n = 0;

	List<Student> aList = new ArrayList<Main.Student>();

	public void handleInput() {
		Scanner in = new Scanner(System.in);

		if (in.hasNext()) {
			n = in.nextInt();
		}

		for (int i = 0; i < n; i++) {
			aList.add(new Student(in.next().trim(), in.nextInt(), in.nextInt(), in.nextInt(), i));
		}
		Collections.sort(aList);

		for (Student s : aList) {
			System.out.println(s.toString());
		}

	}

	class Student implements Comparable<Student> {
		String name = null;
		int year = 0;
		int month = 0;
		int day = 0;
		int number = 0;

		public Student(String name, int year, int month, int day, int number) {
			// TODO Auto-generated constructor stub
			this.name = name;
			this.year = year;
			this.month = month;
			this.day = day;
			this.number = number;
		}

		@Override
		public String toString() {
			// TODO Auto-generated method stub
			return name;
		}

		@Override
		public int compareTo(Student o) {
			// TODO Auto-generated method stub
			if (this.year > o.year) {
				return 1;
			}
			if (this.year < o.year) {
				return -1;
			}
			if (this.month > o.month) {
				return 1;
			}
			if (this.month < o.month) {
				return -1;
			}
			if (this.day > o.day) {
				return 1;
			}
			if (this.day < o.day) {
				return -1;
			}
			if (this.number < o.number) {
				return 1;
			}
			if (this.number > o.number) {
				return -1;
			}
			return 0;
		}

	}
}
发布了88 篇原创文章 · 获赞 27 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/qq_43457125/article/details/104555558
今日推荐