杭电1076-闰年的计算

题目大意:给你一个起始年份和第N个闰年,让你计算N个闰年之后的年份是多少。

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1076


此题是水题,用穷举算法即可求解,而且也不会超时,值得注意的是,不一定是每四年一闰,比如2196年是闰年,而2200年则不是闰年。

代码如下:

#include<iostream>
using namespace std;
int judge(int);
int main()
{
	int T;
	cin>>T; 
	while(T>0)
	{
		int cur_year,Nth,temp;
		int i;
		cin>>cur_year;
		cin>>Nth;
		temp=cur_year;
		while(!judge(temp))   //找到第一个润年 
		    temp++;
		int count=1;
		while(count<Nth)          //用穷举法比较好,因为用4*Nth计算的话不对,并不一定就是每四年一闰,如2200就不是闰年 
		{
			temp=temp+4;
			if(judge(temp))
			   count++;
		 } 
		cout<<temp<<endl;
		T--;
	}
	
 } 
 int judge(int year)
 {
 	if((year%4==0&&year%100!=0)||year%400==0)
 	      return 1;
    else
          return 0;
}

猜你喜欢

转载自blog.csdn.net/liangwgl/article/details/78522005
今日推荐