CCF日期计算(简便解题思路)

思路

平年与闰年的区别:闰年二月比平年多一天。
为了后期更加方便地使用每个月的天数我们可以一开始创建一个month数组来记录一年中每个月的天数。
代码如下:

#include<iostream>
#include<string>
using namespace std;
bool yunnian(int year)//判断是否是闰年 
{
    
    
	return ((year%4==0&&year%100!=0)||year%400==0);
	
}
int main()
{
    
    
	//记录每月中的天数 
	int month[13]={
    
    0,31,28,31,30,31,30,31,31,30,31,30,31}; 
	int year,day;
	cin>>year>>day;
	int i=1,k;//i记录月份k记录余盛天数 
	if(yunnian(year))
	{
    
    
		month[3]++;//如果是闰年第二月天数加一 
		while(day>0)
		{
    
    
			k=day; 
			day-=month[i];
			i++;
		}
	}
	else
	{
    
    
		while(day>0)
		{
    
    
			k=day;
			day-=month[i];
			i++;
		}
	}
	i--;
	cout<<i<<endl;
	cout<<k<<endl;
} 

这道题如果采用month来记录每月天数将极大方便解题。
有什么问题可以留在评论区。

猜你喜欢

转载自blog.csdn.net/Kyrie6c/article/details/105827377