ZZULIOJ 1190: 按出生日期排序(结构体专题),Java
题目描述
送人玫瑰手有余香,小明希望自己能带给他人快乐,于是小明在每个好友生日的时候发去一份生日祝福。小明希望将自己的通讯录按好友的生日排序,这样就查看起来方便多了,也避免错过好友的生日。为了小明的美好愿望,你帮帮他吧。小明的好友信息包含姓名、出生日期。其中出生日期又包含年、月、日三部分信息。输入n个好友的信息,按生日的月份和日期升序输出所有好友信息。
输入
首先输入一个整数n(1<=n<=10),表示好友人数,然后输入n行,每行包含一个好友的信息:姓名(不超过20位),以及三个整数,分别表示出生日期的年月日。文章来源:https://www.toymoban.com/news/detail-633315.html
输出
按过生日的先后(月份和日期)输出所有好友的姓名和出生日期,用空格隔开,出生日期的输出格式见输出样例。文章来源地址https://www.toymoban.com/news/detail-633315.html
样例输入 Copy
3
Zhangling 1985 2 4
Wangliang 1985 12 11
Fangfang 1983 6 1
样例输出 Copy
Zhangling 1985-02-04
Fangfang 1983-06-01
Wangliang 1985-12-11
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
class Friend implements Comparable<Friend> {
String name;
int year;
int month;
int day;
Friend(String name, int year, int month, int day) {
this.name = name;
this.year = year;
this.month = month;
this.day = day;
}
@Override
public int compareTo(Friend o) {
if (this.month == o.month) {
return Integer.compare(this.day, o.day);
}
return Integer.compare(this.month, o.month);
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Friend> friends = new ArrayList<>();
for (int i = 0; i < n; i++) {
String name = sc.next();
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
friends.add(new Friend(name, year, month, day));
}
Collections.sort(friends);
for (Friend friend : friends) {
System.out.printf("%s %04d-%02d-%02d\n", friend.name, friend.year, friend.month, friend.day);
}
}
}
import java.util.ArrayList;
import java.util.Scanner;
class Friend {
String name;
int year;
int month;
int day;
Friend(String name, int year, int month, int day) {
this.name = name;
this.year = year;
this.month = month;
this.day = day;
}
}
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Friend> friends = new ArrayList<>();
for (int i = 0; i < n; i++) {
String name = sc.next();
int year = sc.nextInt();
int month = sc.nextInt();
int day = sc.nextInt();
friends.add(new Friend(name, year, month, day));
}
friends.sort((o1, o2) -> {
if (o1.month == o2.month) {
return Integer.compare(o1.day, o2.day);
}
return Integer.compare(o1.month, o2.month);
});
for (Friend friend : friends) {
System.out.printf("%s %04d-%02d-%02d\n", friend.name, friend.year, friend.month, friend.day);
}
}
}
到了这里,关于ZZULIOJ 1190: 按出生日期排序(结构体专题),Java的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!