CCF CSP 编程题目和解答-----试题名称:日期计算-------201509-2

92 阅读1分钟

问题描述

试题编号:201509-2
试题名称:日期计算
时间限制:1.0s
内存限制:256.0MB
问题描述:问题描述  给定一个年份y和一个整数d,问这一年的第d天是几月几日?   注意闰年的2月有29天。满足下面条件之一的是闰年:   1) 年份是4的整数倍,而且不是100的整数倍;   2) 年份是400的整数倍。输入格式  输入的第一行包含一个整数y,表示年份,年份在1900到2015之间(包含1900和2015)。   输入的第二行包含一个整数d,d在1至365之间。输出格式  输出两行,每行一个整数,分别表示答案的月份和日期。样例输入2015 80样例输出3 21样例输入2000 40样例输出2    9

 

\

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

bool bigmonth(int i,int& d)
{
	bool end=false;
	if(d>31)
				{
					d-=31;
				}
				else
				{
					end=true;
					cout<<i<<endl<<d<<endl;					
				}
				
				
	return end;
}
int smallmonth(int i,int& d)
{
	bool end=false;
	if(d>30)
				{
					d-=30;
				}
				else
				{
					end=true;
					cout<<i<<endl<<d<<endl;					
				}
				
				
	return end;
}

int main()
{
	int y,d;
	cin>>y>>d;
	
	bool flag=false;
	if((y%4==0&&y%100!=0)||(y%400==0))
		flag=true;
	
	
	bool end=false;
	for(int i=1;i<=12;i++)
	{
		switch(i)
		{
			case 1:
			case 3:
			case 5:
			case 7:
			case 8:
			case 10:
			case 12:
			{
				end=bigmonth(i,d);
				break;
			}
			case 2:
			{
				if(flag)
				{
					if(d>29)
					  d-=29;
				  else{
					  end=true;
					cout<<i<<endl<<d<<endl;	
				  }
				}
				else
				{
					if(d>28)
					  d-=28;
				  else{
					  end=true;
					cout<<i<<endl<<d<<endl;	
				  }
				}
				break;
			}
			case 4:
			case 6:
			case 9:
			case 11:
			{
				end=smallmonth(i,d);
				break;
			}
		}
		if(end)
			break;
	}
	
	//system("pause");
}


\