原题链接:蓝桥杯算法提高VIP-任意年月日历输出
; import java.util.Calendar; import java.util.Scanner; public class Main2 { public static void main(String[] args) { // Calendar calendar = Calendar.getInstance(); Scanner sc = new Scanner(System.in); // System.out.print("请输入年份"); // System.out.print("请输入月份"); int year = sc.nextInt(); int month = sc.nextInt(); int days = DaysReturn(year, month); Print(year, days, month); } static boolean judge(int year) {// 判断是不是闰年 if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return true; else return false; } static int DaysReturn(int year, int month)// 找出输入的日期的月份的一号距离2007年1月1日有多少天 { int sum = 0;// 统计一年内的天数 int sum1 = 0;// 统计间隔了多少年的天数 if (judge(year)) { switch (month) { case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 31 + 29; break; case 4: sum = 31 + 29 + 31; break; case 5: sum = 31 + 29 + 31 + 30; break; case 6: sum = 31 + 29 + 31 + 30 + 31; break; case 7: sum = 31 + 29 + 31 + 30 + 31 + 30; break; case 8: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31; break; case 9: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30; break; case 11: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12: sum = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; } } else { switch (month) { case 1: sum = 0; break; case 2: sum = 31; break; case 3: sum = 31 + 28; break; case 4: sum = 31 + 28 + 31; break; case 5: sum = 31 + 28 + 31 + 30; break; case 6: sum = 31 + 28 + 31 + 30 + 31; break; case 7: sum = 31 + 28 + 31 + 30 + 31 + 30; break; case 8: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31; break; case 9: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31; break; case 10: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30; break; case 11: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; break; case 12: sum = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; break; } } for (int i = 2007; i < year; i++) { if (judge(i)) { sum1 += 366; } else sum1 += 365; } sum1 += sum;// 总天数 return sum1; } static void Print(int year, int days, int month) { int x = days % 7 + 1;// 找出这个月的1号是星期几 int monthdays;// 这个月有多少天 System.out.println("---------------------"); System.out.println(" Su Mo Tu We Th Fr Sa"); System.out.println("---------------------"); if (month == 2) { if (judge(year)) monthdays = 29; else monthdays = 28; } else if (month == 4 || month == 9 || month == 6 || month == 11) { monthdays = 30; } else monthdays = 31; if (x == 7) System.out.printf("%3d", 1);// 针对每个月一号不同的星期制定不同的输出格式 if (x == 1) System.out.printf("%6d", 1); if (x == 2) System.out.printf("%9d", 1); if (x == 3) System.out.printf("%12d", 1); if (x == 4) System.out.printf("%15d", 1); if (x == 5) System.out.printf("%18d", 1); if (x == 6) System.out.printf("%21d", 1); for (int i = 2; i <= monthdays; i++) { System.out.printf("%3d", i); if ((i + (x % 7)) % 7 == 0 && i < monthdays) System.out.printf("\n");// 注意如果一个月的最后一号是星期6则无需空行 } System.out.println(); System.out.println("---------------------"); } }
解题思路:
注意事项:
参考代码:
0.0分
0 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复