以前,习惯性第采用switch语句来对月份进行判断,然后整体计算天数。今天尝试另外一种方式来求解。
#include <iostream>
using namespace std;
int Sum(int year,int mon, int day)
{
int k, l;
int tab[2][13] = {{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,30,29,31,30,31,30,31,31,30,31,30,31}};
k = ((year%4==0 && year%100!=0) || (year %400 == 0));
for (l=0; l<mon; l++)
{
day += tab[k][l];
}
return day;
}
int main()
{
int year, mon, day;
cin>>year>>mon>>day;
int sum = Sum(year, mon, day);
cout<<sum<<endl;
return 0;
}