杭电水题之1076

来源:互联网 发布:环球人物和看天下知乎 编辑:程序博客网 时间:2024/05/01 17:40

这题就是判断闰年的题,其实是个很简单的题,只是当时我做的时候没有想好,想法不对,导致了很久都没有做出来,很久之后才明白,里面有两点是需要注意的一点是

Note: if year Y is a leap year, then the 1st leap year is year Y。.这句话比如说2004这就是第一年。第二点就是你需要注意的2100这类的不属于闰年的年份,我当初没有想好是走了弯路,当然了如果是采用下面的第二种方法就不会有这种错误。提供几组数据:

5
126 888
3784
125 777
3328
127 666
2868
128 555
2412
2100 353
3556

这几组对了基本就可以过了。下面提供两种可以AC的代码进攻参考。

#include<iostream>

using namespace std;
int main()
{
   int N,m,n,s,i;
   cin>>N;
   while(N--)
   {
  cin>>m>>n;
  s=0;
  if(m%4==0|| m%400==0)
  {   if(m%100==0)
      s+=m+4;
    else s+=m;
  for(i=0;i<n-1;i++)
{
s+=4;
if((s%100==0)&&(s%400!=0) )
s+=4;
if(s%100==0&&s%400!=0)
s+=4;
  }
  }
  else if(((m+1)%4==0)|| (m+1)%400==0)
  {   if((m+1)%100==0)
  s+=m+1+4;
  else  s+=m+1;
        for(i=0;i<n-1;i++)
{ s+=4;
if((s%100==0 )&&(s%400!=0))
s+=4;
if(s%100==0&&s%400!=0)
s+=4;
}




  }
else if(((m+2)%4==0)||(m+2)%400==0)
{   if((m+2)%100==0)
s+=m+2+4;
  else s+=m+2;
for(i=0;i<n-1;i++)
{
                         s+=4;
if((s%100==0 )&&(s%400!=0))
                     s+=4;
if((s%100==0)&&(s%400!=0))
                 s+=4;
}


}
else if(((m+3)%4==0)||(m+3)%400==0)
{   if((m+3)%100==0)
s+=m+3+4;
else s+=m+3; 
             for(i=0;i<n-1;i++)
{
    s+=4;
           if((s%100==0 )&&(s%400!=0))
    s+=4;
if((s%100==0)&&(s%400!=0))
s+=4;


}
}


cout<<s<<endl;
   }
  return 0;


}

代码二:
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
while(n--)
{
int a,b,i=0,num;
scanf("%d%d",&a,&b);
while(b!=0)
{
num=a+i;
i++;
if((num%4==0&&num%100!=0)||num%400==0)
{
b--;
}
}
printf("%d\n",num);
}
return 0;
}


   
    
原创粉丝点击