zoj 1033 与其说是搜索,不如说是枚举

zoj 与其说是搜索,不如说是枚举,只不过是通过搜索来实现的罢了。

主要是要注意好闰年的判断,特别是要注意好一串数字的划分。

题意其实我也看了一个晚上,才渐渐的看懂。

题意:

给你一个字符串,其中包含数字信息,年月日,如果还有数字意外的字符,那么就一定有两个字符,将年月日三中信息隔开

如果里面没有其他字符,那么就只有数字,其中分配给月,日每个需要两个数字字符,而分配给年则需要2,4个字符来表示

将限制条件写好,自然也就ac了。过程可能不好受,但是都是这么过来的。

  1 #include<iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cmath>
  6 #include<set>
  7 using std::cin;
  8 using std::cout;
  9 using std::endl;
 10 using std::set;
 11 set<int> ans;
 12 int const N = 20;
 13 int month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
 14 int Date;
 15 char tmp[N];
 16 bool Isleap(int y)
 17 {
 18      return (y%400==0&&y%100==0)||(y%4==0&&y%100!=0);
 19 }
 20 int md(int y,int m)
 21 {
 22      return (m==2&&Isleap(y))?month[m]+1:month[m];
 23 }
 24 bool ymd(int y,int m,int d)
 25 {
 26      return (y>=1700&&y<=2299&&m>=1&&m<=12&&d>=1&&d<=md(y,m));
 27 }
 28 int cal_date(int y,int m,int d)
 29 {
 30     int cnt=0;
 31     for(int i = 1700; i < y; i++)
 32         if(Isleap(i))cnt += 366;
 33         else cnt += 365;
 34     for (int i = 1; i < m; i++)
 35         cnt+= md(y,i);
 36     cnt = cnt + d;
 37     return cnt;
 38 }
 39 bool Containf(int len)
 40 {
 41      for(int i=0;i<len;i++)
 42         if(tmp[i]<'0'||tmp[i]>'9')
 43            return true;
 44      return false;
 45 }
 46 void example(int y,int m,int d,int a,int b,int c)
 47 {
 48      if(b<=2&&c<=2)
 49      {
 50         if(a<=2)
 51         for(int i=1700;i<=2200;i+=100)
 52         {
 53             if(ymd(i+y,m,d))
 54             {
 55                int cnt=cal_date(y+i,m,d);
 56                cnt=cnt-Date;
 57                ans.insert(cnt);
 58             }
 59         }
 60         if(a==4)
 61         {
 62            if(ymd(y,m,d))
 63            {
 64               int cnt=cal_date(y,m,d);
 65               cnt=cnt-Date;
 66               ans.insert(cnt);
 67            }
 68         }
 69      }
 70 }
 71 void function(int y,int m,int d,int a,int b,int c)
 72 {
 73      example(y,m,d,a,b,c);
 74      example(y,d,m,a,c,b);
 75      example(m,y,d,b,a,c);
 76      example(m,d,y,b,c,a);
 77      example(d,y,m,c,a,b);
 78      example(d,m,y,c,b,a);
 79 }
 80 int main()
 81 {
 82     int T,len,y,m,d,Case=0;
 83     Date=cal_date(2001,11,4);
 84     cin>>T;
 85     while(T--)
 86     {
 87           ans.clear();
 88           cin>>tmp;
 89           len=strlen(tmp);
 90           y=m=d=0;
 91           if(Containf(len))
 92           {
 93              int i=0,a=0,b=0,c=0;
 94              for(;tmp[i]<='9'&&tmp[i]>='0';i++)y=y*10+tmp[i]-'0',a++;
 95              i++;
 96              for(;tmp[i]<='9'&&tmp[i]>='0';i++)m=m*10+tmp[i]-'0',b++;
 97              i++;
 98              for(;i<len;i++)d=d*10+tmp[i]-'0',c++;
 99              function(y,m,d,a,b,c);
100           }
101           else
102           {
103              for(int i=1;i<len-1;i++)
104              {
105                  for(int j=i+1;j<len;j++)
106                  {
107                      y=m=d=0;
108                      for(int k=0;k<i;k++)y=y*10+tmp[k]-'0';
109                      for(int k=i;k<j;k++)m=m*10+tmp[k]-'0';
110                      for(int k=j;k<len;k++)d=d*10+tmp[k]-'0';
111                      function(y,m,d,i,j-i,len-j);
112                  }
113              }
114           }
115           printf("Scenario #%d:
",++Case);
116           if(ans.empty())
117           {
118              cout<< "Illegal date"<<endl<<endl;
119              continue;
120           }
121           set<int>::iterator rit;
122           for(rit=ans.begin();rit!=ans.end();rit++)
123               cout<<*rit<<endl;
124           cout<<endl;
125     }
126     return 0;
127 }
View Code
原文地址:https://www.cnblogs.com/nuoyan2010/p/3188973.html