超级码力-闰年(模拟+思维)

42 篇文章 1 订阅
14 篇文章 0 订阅

世 上 没 有 绝 望 的 处 境
只 有 对 处 境 绝 望 的 人

题目

题目描述

已知现在是y1年m1月d1日,过x了天后,到了y2年m2月d2日。但是你现在只知道m1,d1,m2,d2和x。请你判断一下y1是不是闰年呢?
输入数据为一个二维的List,每一个小List代表一组询问,List从下标为0到4的值分别代表m1,d1,m2,d2和x。
你需要返回一个字符串,按照询问顺序,回答P,R或?,分别代表y1一定是平年,y1一定是闰年,无法确定y1是闰年还是平年。

Sample Input

4
2 28 3 2 2
2 28 3 1 2
12 31 1 1 1
2 16 1 23 341

Sample Output

PR?P

解题思路

总所周知,两个闰年不会连在一起
那么
连续两年的情况有
平润
润平
平平
这两种情况
而且,时长不会超过一年。(假设两年足够)

我们通过假设可能性来判断。

AC时刻

#include<iostream>
#include<vector>
#pragma GCC optimize(2)
using namespace std;
typedef long long ll;
inline ll read()
{
	ll c = getchar(), Nig = 1, x = 0;
	while (!isdigit(c) && c != '-')c = getchar();
	if (c == '-')Nig = -1, c = getchar();
	while (isdigit(c))x = ((x << 1) + (x << 3)) + (c ^ '0'), c = getchar();
	return Nig * x;
}
#define read read()
class Solution
{
public:
	string guessYear(vector<vector<int>>& inputQueries)
	{
		string ans = "";
		int len = inputQueries.size();
		int day[3][25] =
		{
			{0,31,28,31,30,31,30,31,31,30,31,30,31,31,29,31,30,31,30,31,31,30,31,30,31 },//平润
			{0,31,28,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31 },//平平
			{0,31,29,31,30,31,30,31,31,30,31,30,31,31,28,31,30,31,30,31,31,30,31,30,31 }//润平
		};
		for (int i = 0; i < len; i++)
		{
			int a, b, c, d, x;
			int na, nb, nx;
			bool judge[3];
			bool P, R;
			a = inputQueries[i][0];
			b = inputQueries[i][1];
			c = inputQueries[i][2];
			d = inputQueries[i][3];
			x = inputQueries[i][4];
			for (int j = 0; j < 3; j++)
			{
				na = a, nb = b, nx = x;
				if (nb > day[j][na])
				{
					na++;
					nb = 1;
				}
				while (nx--)
				{
					nb++;
					if (nb > day[j][na])
					{
						na++;
						nb = 1;
					}
				}
				if (na > 12)na -= 12;
				if (na == c && nb == d)judge[j] = 1;
				else judge[j] = 0;
			}
			if (judge[0] || judge[1])P = 1;
			else P = 0;
			R = judge[2];
			if (P && R)ans += "?";
			else if (P)ans += "P";
			else if (R)ans += "R";
		}
		return ans;
	}
};
int main()
{
	Solution res;
	vector<vector<int>>q;
	int n = read;
	for (int i = 0; i < n; i++)
	{
		vector<int>temp;
		for (int j = 0; j < 5; j++)
			temp.push_back(read);
		q.push_back(temp);
	}
	cout << res.guessYear(q) << endl;
	return 0;
}
/*
4
2 28 3 2 2
2 28 3 1 2
12 31 1 1 1
2 16 1 23 341
*/

我觉着一想到跑三次就很容易想到怎么处理了,有问题的话欢迎大家在讨论区留言。

By-轮月

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Round moon

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值