题解 | #计算日期到天数转换#
计算日期到天数转换
http://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
#include<iostream>
#include<vector>
#include<map>
using namespace std;
int main()
{
int year,month,day;
while(cin>>year>>month>>day)
{
map<int,int>mpday;
mpday[1]=31;
mpday[3]=31;
mpday[4]=30;
mpday[5]=31;
mpday[6]=30;
mpday[7]=31;
mpday[8]=31;
mpday[9]=30;
mpday[10]=31;
mpday[11]=30;
mpday[12]=31;
if((year%4==0&&year%100!=0)||year%400==0)
{
mpday[2]=29;
}
else
{
mpday[2]=28;
}
int res=0;
for(int i=1;i<month;i++)
{
res+=mpday.find(i)->second;
}
res+=day;
cout<<res<<endl;
}
return 0;
}