解题思路:
计算出每个日期的总的天数并将结果作为键值对的键存入treeMap中,利用treemap的属性进行自动键的排序
注意事项:
参考代码:
public class 题目1227日期排序 { public static int calDays(String[] split) { int sum=0; int m = Integer.parseInt(split[0]); int d = Integer.parseInt(split[1]); int y = Integer.parseInt(split[2]); //判断是否是闰年(闰年有366 平年365) for (int i = 0; i < y; i++) { if (i % 400 == 0 || (i % 100 != 0 && i % 4 == 0)) { sum += 366; } else { sum += 365; } } for (int i = 1; i < m; i++) { if (i == 1 || i == 3 || i == 5 || i == 7 || i == 8 || i == 10 || i == 12) { sum+=31; } else if (i == 4 || i == 6 || i == 9 || i == 11) { sum += 30; } else if ((y % 400 == 0 || (y % 100 != 0 && y % 4 == 0)) && i == 2) { sum += 29; } else { sum += 28; } } sum+=d; return sum; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); TreeMap<Integer, String> map = new TreeMap<>(); int count=0; while (scanner.hasNext()) { count++; String s = scanner.next(); String[] split = s.split("/"); int days = calDays(split); map.put(days, s); if (count == 6) { break; } } Iterator<Map.Entry<Integer, String>> iterator = map.entrySet().iterator(); while (iterator.hasNext()) { System.out.println(iterator.next().getValue()); } } }
0.0分
2 人评分
C语言训练-排序问题<1> (C语言代码)浏览:1351 |
简单的a+b (C语言代码)浏览:647 |
C语言训练-计算1~N之间所有奇数之和 (C语言代码)浏览:654 |
C语言训练-计算t=1+1/2+1/3+...+1/n (C语言代码)浏览:869 |
C语言训练-计算一个整数N的阶乘 (C语言代码)浏览:936 |
用筛法求之N内的素数。 (C语言代码)浏览:1260 |
C语言程序设计教程(第三版)课后习题6.1 (C语言代码)浏览:702 |
回文数字 (C语言代码)浏览:2510 |
C语言程序设计教程(第三版)课后习题5.4 (C语言代码)浏览:563 |
理财计划 (C语言代码)浏览:468 |