文章

49

粉丝

49

获赞

8

访问

11.6k

头像
日期计算 题解:C++
P1051 中南大学机试题
发布于2024年3月8日 19:02
阅读数 328

谁知道写个这种题写那么久,还是要多练!!!

#include<iostream>
using namespace std;

const int s[12] = { 31,28,31,30,31,30,31,31,30,31,30,31 };

struct date
{
	int year;
	int month;
	int day;
};

bool is_goodyear(int n)
{
	if (n % 400 == 0 || (n % 4 == 0 && n % 100 != 0))
	{
		return true;
	}
	return false;
}

bool is_truedate(int y, int m, int d)
{
	if (y > 0 && m > 0 && m < 13 && d > 0 && d <= s[m - 1])
		return true;
	else if (is_goodyear(y) && m == 2 && d == 29)
		return true;
	else
		return false;
}

int main()
{
	int y, m, d;
	while (cin >> y >> m >> d)
	{
		date a;
		a.year = y;
		a.month = m;
		a.day = d;
		if (!is_truedate(a.year, a.month, a.day))
		{
			cout << "Input error!" << endl;
		}
		else
		{
			int r = 0;
			while (a.month != 0)
			{
				a.month--;
				r += s[a.month - 1];
			}
			r += a.day;
			if (is_goodyear(a.year) &...
登录查看完整内容


登录后发布评论

暂无评论,来抢沙发