Week2 #4 Tomorrow never knows?
作者:互联网
编程题#4:Tomorrow never knows?
来源: POJ (Coursera声明:在POJ上完成的习题将不会计入Coursera的最后成绩。)
注意: 总时间限制: 1000ms 内存限制: 65536kB
描述
甲壳虫的《A day in the life》和《Tomorrow never knows》脍炙人口,如果告诉你a day in the life,真的会是tomorrow never knows?相信学了计概之后这个不会是难题,现在就来实现吧。
读入一个格式为yyyy-mm-dd的日期(即年-月-日),输出这个日期下一天的日期。可以假定输入的日期不早于1600-01-01,也不晚于2999-12-30。
输入
输入仅一行,格式为yyyy-mm-dd的日期。
输出
输出也仅一行,格式为yyyy-mm-dd的日期
样例输入
2010-07-05样例输出
2010-07-06提示
闰年的标准:
(1)普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1901年不是闰年)
(2)世纪年能被400整除的是闰年。(如2000年是闰年,1100年不是闰年)
可以利用一个字符变量吃掉输入的短横线(减号),输出时请活用setfill和setw 控制符。
#include<iostream> #include<iomanip> using namespace std; int main() { int year; int month; int day; char str1; char str2; cin >> year >> str1 >> month >> str2 >> day; int next_month=0; int next_year=0; int next_day=0; int flag=0;//是否是闰年的标志 if (((year % 100 == 0) && (year % 400 == 0)) || ((year % 100 != 0) && (year % 4 == 0))) flag = 1; if (month == 1) { if (day < 31) { next_day = day + 1; next_year = year; next_month = next_month; } else { next_month = month + 1; next_day = 1; next_year = year; } } if (month == 2) { if (flag == 1) { if (day < 29) { next_day = day + 1; next_year = year; next_month = month; } else { next_month = month + 1; next_day = 1; next_year = year; } } else { if (flag == 0) { if (day < 28) { next_day = day + 1; next_month = month; next_year = year; } else { next_month = month + 1; next_day = 1; next_year = year; } } } } if (month == 3) { if (day < 31) { next_day = day + 1; next_month = month; next_year = year; } else { next_month = month + 1; next_day = 1; next_year = year; } } if (month == 4) { if (day < 30) { next_year = year; next_month = month; next_day = day + 1; } else { next_month = month + 1; next_day = 1; next_year = year; } } if (month == 5) { if (day < 31) { next_month = month; next_year = year; next_day = day + 1; } else { next_year = year; next_month = month + 1; next_day = 1; } } if (month == 6) { if (day < 30) { next_year = year; next_month = month; next_day = day + 1; } else { next_year = year; next_month = month + 1; next_day = 1; } } if (month == 7) { if (day < 31) { next_year = year; next_month = month; next_day = day + 1; } else { next_month=month+1; next_day = 1; next_year = year; } } if (month == 8) { if (day < 31) { next_day = day + 1; next_month = month; next_year = year; } else { next_year = year; next_month=month+1; next_day = 1; } } if (month == 9) { if (day < 30) { next_day = day + 1; next_year = year; next_month = year; } else { next_month=month+1; next_day = 1; next_year = year; } } if (month == 10) { if (day < 31) { next_day = day + 1; next_year = year; next_month = month; } else { next_month=month+1; next_day = 1; next_year = year; } } if (month == 11) { if (day < 30) { next_day = day + 1; next_month = month; next_year = year; } else { next_month=month+1; next_day = 1; next_year = year; } } if (month == 12) { if (day < 31) { next_day=day+1; next_month = month; next_year = year; } else { next_year=year+1; next_month = 1; next_day = 1; } } cout << next_year << str1 << setw(2) << setfill('0') << next_month << str2 << setw(2) << setfill('0') << next_day << endl; return 0; }
代码写的又臭又长;中间错了好几次,改了好几次,这样写工作量太大,出错也难排错;考虑将每月的天数做成一个对应天数的数组,可能会简化一点,也许只需要做一次对月份的if判断就可以,具体还没有操作过。
标签:int,never,month,next,else,year,Week2,Tomorrow,day 来源: https://www.cnblogs.com/xyhzq99/p/11644306.html