B. Months and Years(题解)

来源:互联网 发布:vb.net 安卓 编辑:程序博客网 时间:2024/06/08 02:30

原题地址:http://codeforces.com/contest/899/problem/B
题意:给出一串数字(1<=n<=24),判断是否可能是某一年或几年中的连续的月份

  1. 题目很水,因为n的数值很小,只要暴力枚举所有情况,一一比较就可以
  2. 唯一的坑点就在与n是<=24的,这就意味着这出现的数据是有可能的三年的数据,而不是两年。(因此要列举6年每个月的天数,即平平平闰平平)
    下面给出代码
#include <bits/stdc++.h>using namespace std;int val[72]= {31,28,31,30,31,30,31,31,30,31,30,31,              31,28,31,30,31,30,31,31,30,31,30,31,              31,28,31,30,31,30,31,31,30,31,30,31,              31,29,31,30,31,30,31,31,30,31,30,31,              31,28,31,30,31,30,31,31,30,31,30,31,              31,28,31,30,31,30,31,31,30,31,30,31             };//一一列举所有月份int main(){    int n,a[25];    scanf("%d",&n);    for(int i=0; i<n; i++)    {        scanf("%d",&a[i]);    }    for(int i=0; i<=72-n; i++)    {        if(a[0]==val[i])        {            bool ok=true;            int t=0;            for(int j=i; t<n; j++,t++)            {                if(a[t]!=val[j]) ok=false;            }            if(ok==true)            {                printf("YES\n");                return 0;            }        }    }    printf("NO\n");    return 0;}