文章

4

粉丝

407

获赞

4

访问

35.6k

头像
想请问下各位,这道题,我为啥只过了66%,一直找不到原因
P1446 北京理工大学机试题
发布于2020年3月21日 16:19
阅读数 8.0k

#include <iostream>
#include<cstdio>
using namespace std;
int f[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
int n;
cin>>n;
while(n--)
{
    int year,month,day,plus_day;
cin>>year>>month>>day>>plus_day;
//判断平年或闰年
if((year%4==0||year%400==0)&&(year%100!=0))
{
    f[2]=29;
}
else f[2]=28;
//计算这一年的总天数
int sum=0;
for(int i=0;i<13;i++)
{
    sum+=f[i];
}

//该日期是这一年的多少天
int ans=day;
for(int i=1;i<month;i++)
{
    ans+=f[i];
}
ans+=plus_day;
//加上plus_day后,天数在/不在该年内
if(ans<=sum)
{
int j=1;
		while(ans-f[j]>0){
			ans-=f[j++];
		}
		printf("%04d-%02d-%02d\n",year,j,ans);
}
else{

    int tmp=ans-sum;
    int j=1;
		while(tmp-f[j]>0){
			tmp-=f[j++];
		}
    year+=1;
    printf("%04d-%02d-%02d\n",year,j,tmp);
}
}
//cin>>n;


}

 

登录查看完整内容


登录后发布评论

1 条评论
寂寞圣哲
2020年4月2日 19:37

你新的一年是否判断它是否为闰年了?好像只有开始输入的那年判断了

赞(1)