主页 > 开源代码  > 

P8651[蓝桥杯2017省B]日期问题--注意日期问题中2月的天数/if是否应该连用

P8651[蓝桥杯2017省B]日期问题--注意日期问题中2月的天数/if是否应该连用

P8651 [P8651 [蓝桥杯 2017 省 B] 日期问题--注意日期问题中2月的天数 / if是否应该连用 题目 分析代码

题目

分析

代码中巧妙的用到3重循环,完美的解决了输出的顺序问题【题目要求从小到大】

需要注意的是2月的值,在不同的年份中应该更新2月的值

还有就是最后的3个if【T^T,我一开始写的3个if语句】,要么改成else if,要么把判定的条件改成用“||”连接【害我看半天T^T】,不然用3个if回导致重复输出

代码 #include <iostream> #include <vector> #include <set> #include <string> #include <algorithm> #include <math.h> #include <queue> #include <climits> // 包含INT_MAX常量 #include <cctype> using namespace std; int month[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; int check(int x) { return (x % 4 == 0 && x % 100 != 0) || (x % 400 == 0); } int main() { int a, b, c; scanf("%d/%d/%d", &a, &b, &c); //循环从早到晚,巧妙满足了题目要求的从早到晚排列 for (int year = 1960; year <= 2059; year++) { if (check(year)) month[2] = 29; else month[2] = 28; for (int mth = 1; mth <= 12; mth++) { for (int day = 1; day <= month[mth]; day++) { if (a == year % 100 && b == mth && day == c) printf("%d-%02d-%02d\n", year, mth, day); else if (a == mth && b == day && c == year % 100 ) printf("%d-%02d-%02d\n", year, mth, day); else if (a == day && b == mth && c == year % 100 ) printf("%d-%02d-%02d\n", year, mth, day); } } } return 0; }
标签:

P8651[蓝桥杯2017省B]日期问题--注意日期问题中2月的天数/if是否应该连用由讯客互联开源代码栏目发布,感谢您对讯客互联的认可,以及对我们原创作品以及文章的青睐,非常欢迎各位朋友分享到个人网站或者朋友圈,但转载请说明文章出处“P8651[蓝桥杯2017省B]日期问题--注意日期问题中2月的天数/if是否应该连用