First Date |
Time Limit: 3000ms, Special Time Limit:7500ms,Memory Limit:65536KB |
Total submit users: 81, Accepted users:52 |
Problem 12952 : No special judgement |
Problem description |
Given the last day for which the Julian calendar is in effect for some country (expressed as a Julian date), determine the next day’s Gregorian date, i.e., the first date that uses the Gregorian calendar. |
Input |
For each test case, the input consists of one line containing a date in the Julian calendar, formatted as YYYY-MM-DD. This date will be no earlier than October 4, 1582, and no later than October 18, 9999. The given date represents the last day that the Julian calendar is in effect for some country. |
Output |
For each test case, print the first Gregorian date after the calendar transition. |
Sample Input |
1582-10-04 1752-09-02 1900-02-25 1923-02-15 |
Sample Output |
1582-10-15 1752-09-14 1900-03-10 1923-03-01 |
Problem Source |
NWERC 2013 |
/*
题目:F:First Date
题意:J 历规则:能被4整除为闰年 G 历规则:能被4整除但不能被100整除为闰年,或者能被400整除也为闰年。 给出 J 历的日期 求对应 G 历的日期
思路:求出给的年份的前一年两种日历相差的天数x,然后计算给出日期的年份过去的天数y,总天数z=x+y,然后将总天数z分给给出的年份。
感想:比赛时看四个小时的题目就是没看懂,英语实在太差。赛后知道题意后做了,一开始由于最后一年需要特殊处理而且情况比较复杂也容易糊涂所以一直WA。最后改了处理的方法,基本上不需要去怎么特殊处理,也很容易懂,所以才贴出来。这里我是按天分下去的还可以优化下按月分下去。
*/
#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<iterator>
#include<string>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<algorithm>
using namespace std;
#define LL long long
#define inf 1<<29
int day[13] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 不是闰年
int day1[13] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 闰年
int fun(int x)
{
int a = 0, b = 0;
a = x / 4 - 1582 / 4;
b = (x / 4 - x / 100 + x / 400) - (1582 / 4 - 1582 / 100 + 1582 / 400);
return a - b + 11;
}
int fun1(int a, int b, int c)
{
int x = 0;
if (a % 4 == 0) // a是闰年
{
for (int i = 1; i < b; i++) x += day1[i];
x += c;
}
else // a不是闰年
{
for (int i = 1; i < b; i++) x += day[i];
x += c;
}
return x;
}
int main()
{
int a, b, c;
while (scanf("%d-%d-%d", &a, &b, &c) != EOF)
{
int x = 0, y = 0;
x = fun(a - 1); // 前 a-1 年产生的日期差
y = fun1(a, b, c); // 求出 a 年过去了的天数
x += y;
b = 1; c = 0;
for (int i = 1; i <= x; i++) // 按 G 历将天数分给a年
{
if ((!(a % 4) && (a % 100)) || !(a % 400)) // 闰年
{
if (c == day1[b])
{
if (b == 12) ++a, b = 1, c = 1;
else ++b,c = 1;
}
else c++;
}
else // 不是闰年
{
if (c == day[b])
{
if (b == 12) ++a, b = 1, c = 1;
else ++b,c = 1;
}
else c++;
}
}
printf("%d-%02d-%02d\n", a, b, c);
}
return 0;
}