【问题描述】输入某年某月某日,判断这一天是这一年的第几天? 程序分析提示:以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天,特殊情况,闰年且输入月份大于3时需考虑多加一天。
【输入形式】按照年月日输入三个数,分别表示年月日,2017 1 1 【输出形式】2017-1-1 is the first day? 【样例输入】2017 2 1 【样例输出】2017-2-1 is the 32th day
//判断第几天
#include <iostream>
using namespace std;
int main()
{
int day,month,year,sum,leap;
cin>>year>>month>>day;
//计算某月以前月份的天数
switch(month){
case 1:sum=0;break;
case 2:sum=31;break;
case 3:sum=59;break;
case 4:sum=90;break;
case 5:sum=120;break;
case 6:sum=151;break;
case 7:sum=181;break;
case 8:sum=212;break;
case 9:sum=243;break;
case 10:sum=273;break;
case 11:sum=304;break;
case 12:sum=334;break;
default:cout<<"data error!"<<endl;break;
}
//月份天数加上这个月的日数
sum=sum+day;
if(year%400==0||(year%4==0&&year%100!=0)) //判断是否闰年
leap=1;
else
leap=0;
if(leap==1&&month>2)sum=sum+1;
switch(sum){
case 1:
cout<<year<<"-"<<month<<"-"<<day<<" is the first day"<<endl;break;
case 2:
cout<<year<<"-"<<month<<"-"<<day<<" is the second day"<<endl;break;
case 3:
cout<<year<<"-"<<month<<"-"<<day<<" is the third day"<<endl;break;
default:
cout<<year<<"-"<<month<<"-"<<day<<" is the "<<sum<<"th day"<<endl;break;}
return 0;
}
|