ブルーブリッジカップの日付計算(シミュレーション+ピット)

飛躍年を検討し、年と月が範囲内にあるかどうかを検討し、重複を削除することを検討します

acwingポータル

// #pragma GCC optimize(2)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <string>
#include <vector>
#include <stack>
#include <map>
#include <sstream>
#include <cstring>
#include <set>
#include <cctype>
#include <bitset>
#define IO                       \
	ios::sync_with_stdio(false); \
	// cout.tie(0);
using namespace std;
// int dis[8][2] = {0, 1, 1, 0, 0, -1, -1, 0, 1, -1, 1, 1, -1, 1, -1, -1};
typedef unsigned long long ULL;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 2e5 + 10;
const int maxm = 2e5 + 10;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int inf = 0x3f3f3f3f;
const LL mod = 1e9 + 7;
const double pi = acos(-1);
int dis[4][2] = {1, 0, 0, -1, 0, 1, -1, 0};
int m[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
struct Node
{
	int y, m, d;
};
bool cmp(Node a, Node b)
{
	if (a.y != b.y)
	{
		return a.y < b.y;
	}
	else
	{
		if (a.m != b.m)
		{
			return a.m < b.m;
		}
		else
			return a.d < b.d;
	}
}
bool check(int a, int b, int c)
{
	bool f = false;
	bool run = false;
	for (int i = 1960; i <= 2059; i++)
	{
		if (i == a)
		{
			f = true;
			break;
		}
	}
	if (f == false || !(b >= 1 && b <= 12))
		return false;
	if ((a % 4 == 0 && a % 100 != 0) || (a % 400 == 0))
		run = true;
	if (run)
	{
		int num = m[b];
		if (b == 2)
			num += 1;
		if (c >= 1 && c <= num)
			return true;
		else
			return false;
	}
	else
	{
		int num = m[b];
		if (c >= 1 && c <= num)
			return true;
		else
			return false;
	}
}

int main()
{
#ifdef WXY
	freopen("in.txt", "r", stdin);
//	 freopen("out.txt", "w", stdout);
#endif
	IO;
	int a, b, c;
	char ch;
	cin >> a >> ch >> b >> ch >> c;
	int x = 1900;
	int y = 2000;
	vector<Node> v;
	if (check(x + a, b, c))
	{
		v.push_back({x + a, b, c});
	}
	if (check(y + a, b, c))
	{
		v.push_back({y + a, b, c});
	}
	int t1 = c, t2 = a, t3 = b;
	if (check(x + t1, t2, t3))
	{
		v.push_back({x + t1, t2, t3});
	}
	if (check(y + t1, t2, t3))
	{
		v.push_back({y + t1, t2, t3});
	}
	t1 = c, t2 = b, t3 = a;
	if (check(x + t1, t2, t3))
	{
		v.push_back({x + t1, t2, t3});
	}
	if (check(y + t1, t2, t3))
	{
		v.push_back({y + t1, t2, t3});
	}
	sort(v.begin(), v.end(), cmp);
	if (v.size() == 0)
		return 0;
	else
	{
		printf("%04d-%02d-%02d\n", v[0].y, v[0].m, v[0].d);
		for (int i = 1; i < v.size(); i++)
		{
			if (v[i].y == v[i - 1].y && v[i].m == v[i - 1].m && v[i].d == v[i - 1].d)
				continue;
			printf("%04d-%02d-%02d\n", v[i].y, v[i].m, v[i].d);
		}
	}
	return 0;
}

 

おすすめ

転載: blog.csdn.net/qq_44115065/article/details/109106018