原题链接:蓝桥杯2017年第八届真题-日期问题
解题思路:
注意事项:
参考代码:
#include <stdio.h> int LeapYear(int year) //判断是否为闰年,闰年2月有29天,平年28天 { if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) return 1; return 0; } int ChoiceYear(int year) //选择是1900年起步还是2000年起步 { if (year >= 60) return year + 1900; return year + 2000; } void rule(int year, int month, int day) //判断是否满足年月日规则,并输出 { int n; year = ChoiceYear(year); if (month >= 1 && month <= 12) { switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12:n = 31; break; case 2: n = 28; if (LeapYear(year)) n++; break; default: n = 30; break; } if (day <= n && day >= 1) { if(month < 10 && day >10) printf("%d-0%d-%d\n", year, month, day); else if(month < 10 && day < 10) printf("%d-0%d-0%d\n", year, month, day); else if (month > 10 && day < 10) printf("%d-%d-%0d\n", year, month, day); else printf("%d-%d-%d\n", year, month, day); } } } int main() { int year, month, day; scanf("%2d/%2d/%2d",&year,&month,&day); //year,month,day //day,month,year //day,year,month int year1 = ChoiceYear(year); int day1 = ChoiceYear(day); if (year1 < day1) { rule(year, month, day); if (month < year) { rule(day, month, year); rule(day, year, month); } else if (month == year) rule(day, month, year); else { rule(day, year, month); rule(day, month, year); } } else if (year < day && month > 12) { rule(day, year, month); } //year,month,day //day,month,year //day,year,month if (year1 == day1) { if (month < year) { rule(year, month, day); rule(day, month, year); rule(day, year, month); } else if (month == year) rule(year, year, year); else if(month > year) { rule(day, year, month); rule(day, month, year); } } //year,month,day //day,month,year //day,year,month if (year1 > day1) { if (month < year) { rule(day, month, year); rule(day, year, month); rule(year, month, day); } else if(month == year) { rule(day, month, year); rule(year, month, day); } else if(month > year) { rule(day, year, month); rule(day, month, year); rule(year, month, day); } } return 0; }
0.0分
1 人评分
C语言网提供由在职研发工程师或ACM蓝桥杯竞赛优秀选手录制的视频教程,并配有习题和答疑,点击了解:
一点编程也不会写的:零基础C语言学练课程
解决困扰你多年的C语言疑难杂症特性的C语言进阶课程
从零到写出一个爬虫的Python编程课程
只会语法写不出代码?手把手带你写100个编程真题的编程百练课程
信息学奥赛或C++选手的 必学C++课程
蓝桥杯ACM、信息学奥赛的必学课程:算法竞赛课入门课程
手把手讲解近五年真题的蓝桥杯辅导课程
发表评论 取消回复