题目
Description
有一些日期,日期格式为“MM/DD/YYYY”。编程将其按日期大小排列。
Input
Output
Sample Input
01/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
Sample Output
01/12/1999
10/21/2003
10/22/2003
02/12/2004
11/30/2005
12/31/2005
代码块
import java.util.Scanner;//输入包
public class Main{
public static void main(String[] args) {
Scanner cn = new Scanner(System.in);//输入流
int[][] num = new int[100][3];//创建一个二位数数组
int[] t = new int[3];
int p = 0;
while (cn.hasNext()) {//判断是否输入结束
String str = cn.next();
String[] da = str.split("/");//进行输入的格式分割
int a = Integer.parseInt(da[0]);//将存在字符串da里的值转化为int类型并存在num数组中
int b = Integer.parseInt(da[1]);
int c = Integer.parseInt(da[2]);
num[p][0] = a;
num[p][1] = b;
num[p][2] = c;
p++;
}
for (int i = 0; i < p - 1; i++) {//先判断日,日期大的往后排
for (int j = 0; j < p - 1 - i; j++) {
if (num[j][1] > num[j + 1][1]) {
t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
for (int i = 0; i < p - 1; i++) {//再判断月份,月份大的往后排
for (int j = 0; j < p - 1 - i; j++) {
if (num[j][0] > num[j + 1][0]) {
t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
for (int i = 0; i < p - 1; i++) {//再判断年份,年份大的往后排
for (int j = 0; j < p - 1 - i; j++) {
if (num[j][2] > num[j + 1][2]) {
t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
//最后输出格式要与输入的格式相符合,用了printf的输出格式
for (int i = 0; i < p; i++)
System.out
.printf("%02d/%02d/%d\n", num[i][0], num[i][1], num[i][2]);
cn.close();//关闭输入流
}
}