编写函数,给出年、月、日后,求该日是该年的第几天。
#include<iostream>
using namespace std;
int a(int y, int m,int d)
{
switch (m)
{
case 1:
return d;
break;
case 2:
return 31 + d;
break;
case 3:
return 31 + 29 + d;
break;
case 4:
return 31 * 2 + 29 + d;
break;
case 5:
return 31 * 2 + 29 + 30 + d;
break;
case 6:
return 31 * 3 + 29 + 30 + d;
break;
case 7:
return 31 * 3 + 29 + 30 * 2 + d;
break;
case 8:
return 31 * 4 + 29 + 30 * 2 + d;
break;
case 9:
return 31 * 5 + 29 + 30 * 2 + d;
break;
case 10:
return 31 * 5 + 29 + 30 * 3 + d;
break;
case 11:
return 31 * 6 + 29 + 30 * 3 + d;
break;
case 12:
return 31 * 6 + 29 + 30 * 4 + d;
break;
}
}
int b(int y, int m, int d)
{
switch (m)
{
case 1:
return d;
break;
case 2:
return 31 + d;
break;
case 3:
return 31 + 28 + d;
break;
case 4:
return 31 * 2 + 28 + d;
break;
case 5:
return 31 * 2 + 28 + 30 + d;
break;
case 6:
return 31 * 3 + 28 + 30 + d;
break;
case 7:
return 31 * 3 + 28 + 30 * 2 + d;
break;
case 8:
return 31 * 4 + 28 + 30 * 2 + d;
break;
case 9:
return 31 * 5 + 28 + 30 * 2 + d;
break;
case 10:
return 31 * 5 + 28 + 30 * 3 + d;
break;
case 11:
return 31 * 6 + 28 + 30 * 3 + d;
break;
case 12:
return 31 * 6 + 28 + 30 * 4 + d;
break;
}
}
int main()
{
int year,month,day;
cin >> year>>month>>day;
if ((year % 4 == 0) && (year % 100 != 0 || year % 400 == 0))
{
cout<<"在该年"<<a(year, month, day)<<"天";
}
else
{
cout<<"在该年"<<b(year, month, day)<<"天";
}
}