14090: 【原4090】日期推算
题目
题目描述
author: 程序设计思想与方法助教组_刘威 原OJ链接:https://acm.sjtu.edu.cn/OnlineJudge-old/problem/4090 ## 问题描述 设计一个程序用于向后推算指定日期经过n天后的具体日期。
输入输出描述
输入
- 输入为长度为8的字符串str和一个正整数n,str前四位表示年份,后四位表示月和日。
输出
- 当推算出的年份大于4位数时,输出"out of limitation!",否则输出8位的具体日期。
程序运行示例1
Sample Input 1
00250709 60000
Sample Output 1
01891017
程序运行示例2
Sample Input 2
19310918 5080
Sample Output 2
19450815
程序运行示例3
Sample Input 3
99980208 999
Sample Output 3
out of limitation!
注意
-
日期的表示必须8位数,年月日不足长度需要添加前缀字符'0'。
-
注意闰年和平年的2月份天数不同。
-
注意判断输出信息是否符合要求。
ligongzzz's solution
#include "cstdio"
#include "iostream"
using namespace std;
int main() {
int year, month, day;
int n;
int months[13] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
scanf("%4d%2d%2d %d", &year, &month, &day, &n);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
months[2] = 29;
else
months[2] = 28;
for (; n > 0; n--) {
if (day + 1 > months[month]) {
day = 1;
if (month + 1 > 12) {
month = 1;
if (year + 1 > 9999) {
cout << "out of limitation!";
return 0;
}
else
year++;
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
months[2] = 29;
else
months[2] = 28;
}
else
month++;
}
else
day++;
}
printf("%04d%02d%02d", year, month, day);
return 0;
}