Input
输入是一个字符串,根据实际情况,一般一行输入不会太长,我们规定串的长度<=256。根据国家标准,合格的日期是YYYYMMDD或者YYYY-MM-DD,我们还可以扩展一下,如果把公元前的年份为负数,那么-YYYYMMDD和-YYYY-MM-DD也算为合格输入,同时规定此时输出加前缀“BC ”,为了使问题简化,特别是简化闰年的计算,我们规定年份的绝对值不超过9999。本问题是多组测试数据。
Output
输出给定日期的向后一天或者“Error !”。输出时都按照“-”作为分隔符。
Sample Input
2015-03-31
12345678
Sample Output
2015-04-01
Error !
注意对输入格式进行检验即可 还有就是不存在公元0年,要特判一下
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
#include<stack>
#include<queue>
#include<vector>
#include<map>
#define fi first
#define se second
#define pb(i) push_back(i)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define mes(a,b) memset(a,b,sizeof(a))
#define sbs(i,j) substr(i,j)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int N = 1e6+10;
const int INF = 0x3f3f3f3f;
const int inf = - INF;
const int mod = 1e9+7;
const double pi = acos(-1.0);
int year=0,month=0,day=0;
char s[505];
int days[2][13]= {0,31,28,31,30,31,30,31,31,30,31,30,31,0,31,29,31,30,31,30,31,31,30,31,30,31};
int is_leap(int x)
{
if(x<0)
x+=1;
if (x%400==0||(x%4==0&&x%100!=0))
return 1;
else
return 0;
}
bool between(int x,int y,int z){
return z>=x&&z<=y;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
while(cin>>s)
{
int flag = 0;
int index = 0;
int year=0,month=0,day=0;
int isLeap=0;
int len=strlen(s);
bool ok=true;
if(s[0]=='-')
{
flag=1;
index=1;
}
int cnt=0;
for(int i=0;i<len;i++) if(s[i]=='-')cnt++;
for(int i=1;i<len;i++){
if(!between(0,9,s[i-1]-'0')&&!(between(0,9,s[i]-'0'))) ok=false;
}
for(int i=index; i<=index+3; i++){
if(!between(0,9,s[i]-'0')) ok=false;
year=year*10+s[i]-'0';
}
if(year>9999||year==0)
ok=false;
if(flag)
year=-year;//判断闰年
isLeap=is_leap(year);
index+=4;
if(s[index]=='-')
index++;
else{if(s[index+1]=='-') ok=false;}
for(int i=index; i<=index+1; i++)
{
if(!between(0,9,s[i]-'0')) ok=false;
month=month*10+s[i]-'0';
}
if(month>12||month<1)
ok=false;//合法性检查
index+=2;
if(s[index]=='-')
index++;
else{if(s[index+1]=='-') ok=false;}
for(int i=index; i<=index+1; i++){
if(!between(0,9,s[i]-'0')) ok=false;
day=day*10+s[i]-'0';
}
index+=2;
/*合法性检查*/
//printf("year=%d,month=%d,day=%d\n",year,month,day);
if(day>days[isLeap][month])
ok=false;
if(len!=8+cnt) ok=false;
if(!ok) /*输入不合法*/
{
printf("Error !\n");
continue;
}
day++;
if(day>days[isLeap][month])
{
day=1;
if(month==12)
{
month=1;
year++;
}
else
month++;
}
if(year>9999||year<-9999)
{
printf("Error !\n");
continue;
}
if(year<=0)
{
if(year!=0)
printf("BC ");
if(year==0)
year=1;
}
year=abs(year);
printf("%04d-%02d-%02d\n",year,month,day);
}
return 0;
}