题解 | #计算日期到天数转换#
计算日期到天数转换
https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded
int main()
{
string str;
getline(cin,str);
int len = str.length();
int num=0;//天数
string str1 = str.substr(0,4);//不读第4个字符
string str2 = str.substr(5,2);//不读第7个字符
string str3 = str.substr(len-2,2);
string str4 = str.substr(0,2);//年份前两个数
string str5 = str.substr(2,2);//年份后两个数
//int year = string;
if(stoi(str1) % 4 == 0)//判断闰年(闰年2月有29天,其余年份为28天)
{
if(stoi(str2) == 1)//
{
num = stoi(str3);
}
else if(stoi(str2) == 2)
{
num = 31 + stoi(str3);
}
else if(stoi(str2) == 3)
{
if(stoi(str5) == 0 && stoi(str4) % 4 == 0)//2000这种闰年
num = 31 + 29 + stoi(str3);
else if(stoi(str5) == 0 && stoi(str4) % 4 != 0)//1900这种平年
num = 31 + 28 + stoi(str3);
else
num = 31 + 29 + stoi(str3);//2012这种闰年
}
else if(stoi(str2) == 4)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + stoi(str3);
else
num = 31 + 29 + 31 + stoi(str3);
}
else if(stoi(str2) == 5)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + stoi(str3);
}
else if(stoi(str2) == 6)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 + stoi(str3);
}
else if(stoi(str2) == 7)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 +30 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 +30 + stoi(str3);
}
else if(stoi(str2) == 8)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + stoi(str3);
}
else if(stoi(str2) == 9)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + stoi(str3);
}
else if(stoi(str2) == 10)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + stoi(str3);
}
else if(stoi(str2) == 11)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + stoi(str3);
}
else if(stoi(str2) == 12)
{
if(stoi(str5) == 0 && stoi(str4) % 4 != 0)
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + stoi(str3);
else
num = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + stoi(str3);
}
}
else//非闰年
{
//判断月份
if(stoi(str2) == 1)//
{
num = stoi(str3);
}
else if(stoi(str2) == 2)
{
num = 31 + stoi(str3);
}
else if(stoi(str2) == 3)
{
num = 31 + 28 + stoi(str3);
}
else if(stoi(str2) == 4)
{
num = 31 + 28 + 31 + stoi(str3);
}
else if(stoi(str2) == 5)
{
num = 31 + 28 + 31 + 30 + stoi(str3);
}
else if(stoi(str2) == 6)
{
num = 31 + 28 + 31 + 30 + 31 + stoi(str3);
}
else if(stoi(str2) == 7)
{
num = 31 + 28 + 31 + 30 + 31 +30 + stoi(str3);
}
else if(stoi(str2) == 8)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + stoi(str3);
}
else if(stoi(str2) == 9)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + stoi(str3);
}
else if(stoi(str2) == 10)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + stoi(str3);
}
else if(stoi(str2) == 11)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + stoi(str3);
}
else if(stoi(str2) == 12)
{
num = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + stoi(str3);
}
}
cout<<num<<endl;
return 0;
}
小天才公司福利 1313人发布
