日期暴力

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).

4
31 31 30 31

Output

Yes

Input

2
30 30

Output

No

Input

5
29 31 30 31 30

Output

Yes

Input

3
31 28 30

Output

No

Input

3
31 31 28

Output

Yes
#include<stdio.h>
#include<iostream>
#include<cmath>
#include<string.h>
#include<stdlib.h>
#include<map>
#include<algorithm>
using namespace std;
int main()
{
    int n,a[200],b,i,j,ge,sum,k,c[100]=
    {
        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
    };
    scanf("%d",&n);
    ge=0;
    for(i=0; i<=n-1; i++)
    {
         scanf("%d",&a[i]);
         if(a[i]==29)
            ge++;
    }
    if(ge>=2)
    {
        printf("No\n");
        return 0;

    }
     ge=0;
    for(i=0; i<=72-n; i++)
    {
        k=i;
        for(j=0; j<=n-1; j++)
        {
            if(a[j]==c[k++])
                ge++;
            else
                break;
        }
        if(ge==n)
        {
            printf("Yes\n");
            return 0;
        }
        else
            ge=0;
    }
    printf("No\n");




}

 

<think>好的,用户想了解关于“暴力解法”的相关概念和实现方法。首先,我需要明确暴力解法的定义和特点。根据引用[1]和[2],暴力解法通常指通过穷举所有可能的情况来解决问题,适用于问题规模较小的情况,虽然效率不高但实现简单。 接下来,需要整理暴力解法的应用场景。比如在算法题中,当输入规模不大时,暴力法可以快速解决问题,例如日期问题或数组操作。引用[3]中的移除元素暴力解法就是两层循环的例子,这符合暴力法的特征。 然后,用户可能需要具体的实现示例。引用[3]提供了Python代码,用两个循环更新数组,这可以作为示例。此外,引用[4]提到字符串匹配的暴力解法,可以举一个双重循环比较字符的例子。 还要考虑用户可能的问题,比如暴力解法的优缺点。优点是简单直观,缺点是时间复杂度高。需要分点说明这些内容,确保结构清晰。 最后,根据用户提供的引用资料,确保引用标识正确添加,比如在提到蓝桥杯暴力拆解时标注[^1],在模拟类题目时标注[^2]等。同时,生成的相关问题需要覆盖性能、优化、应用场景等方面,帮助用户进一步学习。</think>### 暴力解法的概念与实现方法 暴力解法(Brute Force)是一种通过穷举所有可能情况来解决问题的算法策略。其核心思想是**不依赖复杂优化**,直接根据问题描述逐一遍历可能的解空间,找到符合条件的答案。虽然效率较低,但实现简单且逻辑直观,适用于小规模问题或作为其他优化算法的验证基准。 --- #### 一、暴力解法的特点 1. **简单直接**:无需深入分析问题规律,直接模拟操作步骤即可[^2]。 2. **高时间复杂度**:通常涉及多重循环或递归,时间复杂度多为 $O(n^2)$、$O(2^n)$ 等[^3]。 3. **适用范围有限**:适合输入规模较小的问题,例如蓝桥杯中的暴力拆解题[^1]。 --- #### 二、暴力解法的实现示例 ##### 示例1:数组移除元素(引用自) 问题:给定数组 `nums` 和值 `val`,原地移除所有等于 `val` 的元素,返回新长度。 ```python def remove_element(nums, val): i = 0 while i < len(nums): if nums[i] == val: # 发现目标值,将后续元素前移 for j in range(i, len(nums)-1): nums[j] = nums[j+1] nums.pop() # 删除最后一个重复元素 else: i += 1 return len(nums) ``` **说明**:通过两层循环逐个检查并移动元素,时间复杂度为 $O(n^2)$。 ##### 示例2:字符串匹配(引用自[^4]) 问题:在字符串 `haystack` 中查找子串 `needle` 的起始位置。 ```python def str_str(haystack, needle): if not needle: return 0 for i in range(len(haystack) - len(needle) + 1): for j in range(len(needle)): if haystack[i+j] != needle[j]: break if j == len(needle)-1: return i return -1 ``` **说明**:通过两重循环逐字符比对,时间复杂度为 $O(m \cdot n)$($m$ 和 $n$ 为两字符串长度)。 --- #### 三、暴力解法的适用场景 1. **模拟类问题**:如日期计算、图形排版等需按步骤实现的场景。 2. **枚举可能性**:如排列组合、子集生成等小规模穷举问题。 3. **算法验证**:作为优化算法(如动态规划、KMP)正确性的对照基准[^4]。 --- #### 四、暴力解法的优化方向 1. **剪枝策略**:提前终止无效分支(如搜索中遇到不满足条件的情况立即返回)。 2. **空间换时间**:利用哈希表或缓存减少重复计算。 3. **问题分解**:将大问题拆解为可重复利用的小问题。 ---
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值