题解 | #计算日期到天数转换#

计算日期到天数转换

https://www.nowcoder.com/practice/769d45d455fe40b385ba32f97e7bcded

// HJ73-2 计算日期到天数转换.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int main()
{
	vector<int>mr = { 0,31,29,31,30,31,30,31,31,30,31,30,31 };
	vector<int>mp = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
	string s;
	while (getline(cin, s))
	{
		int year, m, d,res=0;
		year = stoi(s.substr(0, 4));
		string tmp = s.substr(5);
		int it = tmp.find(' ');
		m = stoi(tmp.substr(0, it));
		d = stoi(tmp.substr(it));
		//cout << year << " " << m << " " << d << endl;
		if (year % 100 == 0)
		{
			if (year % 400 == 0)
			{
				for (int i = 1; i < m; i++)
				{
					res += mr[i];
				}
				res += d;
			}
			else
			{
				for (int i = 1; i < m; i++)
				{
					res += mp[i];
				}
				res += d;
			}
		}
		else if (year % 4 == 0)
		{
			for (int i = 1; i < m; i++)
			{
				res += mr[i];
			}
			res += d;
		}
		else
		{
			for (int i = 1; i < m; i++)
			{
				res += mp[i];
			}
			res += d;
		}
		cout << res << endl;
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务