题目:
Everybody in Russia uses Gregorian calendar. In this calendar there are 31 days in January, 28 or 29 days in February (depending on whether the year is leap or not), 31 days in March, 30 days in April, 31 days in May, 30 in June, 31 in July, 31 in August, 30 in September, 31 in October, 30 in November, 31 in December.
A year is leap in one of two cases: either its number is divisible by 4, but not divisible by 100, or is divisible by 400. For example, the following years are leap: 2000, 2004, but years 1900 and 2018 are not leap.
In this problem you are given n (1 ≤ n ≤ 24) integers a1, a2, ..., an, and you have to check if these integers could be durations in days of n consecutive months, according to Gregorian calendar. Note that these months could belong to several consecutive years. In other words, check if there is a month in some year, such that its duration is a1 days, duration of the next month is a2 days, and so on.
输入:The first line contains single integer n (1 ≤ n ≤ 24) — the number of integers.
The second line contains n integers a1, a2, ..., an (28 ≤ ai ≤ 31) — the numbers you are to check.
输出:
If there are several consecutive months that fit the sequence, print "YES" (without quotes). Otherwise, print "NO" (without quotes).
You can print each letter in arbitrary case (small or large).
ac代码:
#include <iostream>
using namespace std;
int main() {
string a="181010110101181010110101191010110101181010110101",b="";
a+=a+a+a;
int n;
cin>>n;
for(int i=0;i<n;i++)
{
int tmp;
cin>>tmp;
b+=tmp%10+48;
cout<<b<<endl;
}
if(a.find(b)==-1) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
return 0;
}
解题思路:利用字符串来判断月份是否连续。
步骤:1.定义字符串数组a 字符串数组a每个字符代表一个月份 一共48个字符共代表“平平闰平” 涵盖三种年份过渡方式{平-平,平-闰、闰-平}
2.读入月份 同时%10只留下个位 根据ASCII码表把int类型转化为字符串b
3.利用find函数判断字符串b是否存在于字符串a中