蓝桥杯:枚举、模拟、排序

前言:

这类问题在蓝桥杯中经常出现,没有固定做法,一般难度不算大,但是要考虑很多细节。一般做法是按照题目要求模拟出来,一般不会涉及最优化问题。DP、贪心、DFS、数据结构通常是用来做最优化的,去优化效率。

小结:筛选出不合格的要比筛选出合格的要容易,一般合格比不合格要求严格。

eg:acWing 1229.日期问题

 (1)反推法:通过大部分测试点的答案如下,未ac原因:考虑不周全。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=9;
struct Date
{
    int b1,b2,b3;
    bool operator <(const Date & t)
    {
        if(b1!=t.b1) return b1<t.b1;
        else
        {
            if(b2!=t.b2) return b2<t.b2;
            else{
                return b3<t.b3;
            }
        }
    }
};
char s[N];
Date date[3];
int b1,b2,b3;
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
bool check(int y,int m,int d)
{

    if(m>12) return false;
    if(m!=2&&d>days[m]||d==0)  return false;
    if(m==2) 
    {
        bool k=y%4==0&&y%100||y%400==0;
        if(d>28+k) return false;
    }
    return true;
}
int main()
{
   cin>>s;
   b1=(s[0]-'0')*10+s[1]-'0';
   b2=(s[3]-'0')*10+s[4]-'0';
   b3=(s[6]-'0')*10+s[7]-'0';
   date[0].b1=b1,date[0].b2=b2,date[0].b3=b3;
   date[1].b1=b3,date[1].b2=b1,date[1].b3=b2;
   date[2].b1=b3,date[2].b2=b2,date[2].b3=b1;
   sort(date,date+3);
   for(int i=0;i<3;i++)
   {
     b1=date[i].b1;
     b2=date[i].b2;
     b3=date[i].b3;
     if(check(b1,b2,b3)) 
            {
              if(b1>=60&&b1<=99&&b1>9)
               cout<<"19"<<b1<<"-";
              else if(b1>=60&&b1<=99&&b1<10)
               cout<<"190"<<b1<<"-";
             else if(b1>=0&b1<=59&&b1>9)
               cout<<"20"<<b1<<"-";
             else if(b1>=0&&b1<=59&&b1<10)
              cout<<"200"<<b1<<"-";
             if(b2<10)
              cout<<"0"<<b2<<"-";
             else cout<<b2<<"-";
             if(b3<10)
              cout<<"0"<<b3<<endl;
             else cout<<b3<<endl;
            }
   }
       return 0;
}

(2)正推法:建议用这种方法,列举所有可能答案,然后排除。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int days[13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
bool check(int year,int month,int day)
{
   if(month==0||month>12) return false;
   if(day==0) return false;
   if(month!=2)
   {
       if(day>days[month]) return false;
   }
   else
   {
      int k=year%100&&year%4==0||year%400==0;
      if(day>28+k) return false;
   }
   return true;
}
int main()
{
  int a,b,c;
  scanf("%d/%d/%d",&a,&b,&c);
  for(int i=19600101;i<=20591231;i++)
  {
      int year=i/10000,month =i%10000/100,day=i%100;
      if(check(year,month,day))
      {
          if(year % 100 == a && month == b && day == c ||        // 年/月/日
                month == a && day == b && year % 100 == c ||        // 月/日/年
                day == a && month == b &&year % 100 == c)
                printf("%d-%02d-%02d\n",year,month,day);
      }
  }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值