世 上 没 有 绝 望 的 处 境
只 有 对 处 境 绝 望 的 人
题目
题目描述
已知现在是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
*/
我觉着一想到跑三次就很容易想到怎么处理了,有问题的话欢迎大家在讨论区留言。