朴素做法,总是比正确答案少一天两天,究竟错在哪里?提交只对了1个点
查看原帖
朴素做法,总是比正确答案少一天两天,究竟错在哪里?提交只对了1个点
322285
北京楼主2021/2/3 16:27

以下是代码,我打上了详细的注释增强可读性

//P7075 [CSP-S2020] 儒略日
#include<bits/stdc++.h>
using namespace std;
const int N1=2299159;//-4713.1.1~1582.10.3总天数 
const int N2=1461;//儒略历中每4年的天数
const int N3=146097;//格里高利历中每400年的天数 
const int N4=6299-10;
//从1582.10.3快进到1600.1.1的天数,注意有10天被删掉了
const int N5=36524;//格里高利历每100年的天数
const int N6=1461;//格里高利历每4年的天数 
const int mon1[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//平年月份 
const int mon2[]={0,31,29,31,30,31,30,31,31,30,31,30,31};//闰年月份 
void solve1(long long r);//不到达转换格里高利历时间做法 
void solve2(long long r);//转换到了格里高利历做法 
bool run1(long long y)//儒略历闰年判断函数 
{
	if(y<0)//注意还要判正负
		++y;
	if(y%4==0)return true;
	else return false;
}
bool run2(long long y)//格里高利历闰年判断函数
{
	if((y%4==0&&y%100!=0)||y%400==0)return true;
	return false;
} 
int main()
{
	int Q;
	cin>>Q;
	for(int i=1;i<=Q;++i)
	{
		long long r;
		cin>>r;
		if(r<=N1)solve1(r);//不到达转换格里高利历时间做法 
		else solve2(r);//转换到了格里高利历做法 
	}
	return 0;
}
void solve1(long long r)
{
	long long n1=r/N2;//求出有多少个4年
	r%=N2;//剩余天数 
	long long y=n1*4-4713,m=1,d=1;//求出未加剩余天数的日期 
	if(y>0)++y;//公元零年并不存在
	//cout<<r<<endl;
	
	for(;r>=365;++y)
	{
		if(run1(y)&&r>=366)//如果此时是闰年 
			r-=366;
		else r-=365;
	}
	//cout<<r<<endl;
	
	if(run1(y))//如果此时是闰年 
	{
		for(;r-mon2[m]>0;++m)r-=mon2[m];
	}
	else for(;r-mon1[m]>0;++m)r-=mon1[m];
	
	d+=r;
	if(y>0)printf("%d %d %d\n",d,m,y);
	else printf("%d %d %d BC\n",d,m,-y);
}
void solve2(long long r)
{
	long long y=0,m=0,d=0;
	r-=N1,y=1582,m=10,d=3;//快进到1582.10.3
	if(r>=N4)r-=N4,y=1600,m=1,d=1;//如果天数允许,快进到1600.1.1 
	if(r>365)--r;//1600年也是闰年,而变量N3的400年没算上它 
	//cout<<r<<endl;
	
	long long n1=r/N3;//算出r中有多少个400年
	r%=N3;//求出剩余天数
	y+=n1*400;
	//cout<<r<<endl;
	
	for(;r>=N5;y+=100)
	{
		if(run2(y)&&r>=N5+1)//如果此时是闰年 
			r=r-N5-1;
		else r-=N5;
	}
	//cout<<r<<endl;
	
	for(;r>=N6;y+=4)//算出r中有多少个4年 
	{
		if(run2(y)&&r>=N6+1)//如果此时是闰年 
			r=r-N6-1;
		else r-=N6;
	}
	//cout<<r<<endl;
	
	for(;r>=365;++y)
	{
		if(run2(y)&&r>=366)//如果此时是闰年 
			r-=366;
		else r-=365;
	}
	//cout<<r<<endl;
	
	if(run2(y))//如果此时是闰年 
	{
		for(;r-mon2[m]>=0;++m)r-=mon2[m];
	}
	else for(;r-mon1[m]>=0;++m)r-=mon1[m];
	//cout<<r<<endl;
	
	d+=r;
	printf("%d %d %d\n",d,m,y);
}
//调到一半实在不知道错哪里......——2021.1.29 

上周调了一个上午,今天又调了一个上午,愣是没调出来哪里有问题(我手算都跟正确答案不一样)......总之答案总是会跟大样例缺个一天两天

顺便再分享一下是如何发现错误的呗!让我学学大佬是如何调试的

感谢指教!

2021/2/3 16:27
加载中...