某年的第几天

题目描述

输入年、月、日,计算该天是本年的第几天。

输入描述:

包括三个整数年(1<=Y<=3000)、月(1<=M<=12)、日(1<=D<=31)。

输出描述:

输入可能有多组测试数据,对于每一组测试数据,
输出一个整数,代表Input中的年、月、日对应本年的第几天。
示例1

输入

复制
1990 9 20
2000 5 1

输出

复制
263
122
switch解法:

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int y,d;
int m;
while(cin>>y>>m>>d)
{
int ans=0;
switch(m)
{
case 12:ans+=30;
case 11:ans+=31;
case 10:ans+=30;
case 9:ans+=31;
case 8:ans+=31;
case 7:ans+=30;
case 6:ans+=31;
case 5:ans+=30;
case 4:ans+=31;
case 3:ans+=28;
case 2:ans+=31;
case 1:ans+=0;
}
if((y%400==0||(y%4==0&&y%100!=0))&&m>2)
ans++;
cout<<ans+d<<endl;

}
return 0;
}

还可以这样,写起来更简单:

#include <stdio.h>
const int month[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int main()
{
     for ( int y,m,d;~ scanf ( "%d%d%d" ,&y,&m,&d); printf ( "%d\n" ,d))
    {
         for ( int i=1;i<m;d+=month[i++]);
         if (y%400==0 || (y%100 && y%4==0))
           d+=(m>2?1:0);
     }
     return 0;
}
 

猜你喜欢

转载自www.cnblogs.com/zhanghuawei/p/11930312.html