2 条题解

  • 1
    @ 2024-3-21 11:06:03
    #include <iostream>
    #include <cmath>
    using namespace std;
    int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    
    int main()
    {
    	int cnt = 0;
    	for(int i = 1; i <= 12; i ++ )
    	{
    		int a = i/10, b = i%10;
    		for(int j = 1; j <= days[i]; j ++ )
    		{
    			int c = j/10, d = j%10;
    			if((a + 1 == b && b + 1 == c) || (b + 1 == c && c + 1 == d))
    				cnt++;
    		}
    	}
    	cout<<cnt<<endl;
    }
    

    容易看出顺子和2022没有关系 直接枚举月日即可

    • 1
      @ 2024-3-19 23:55:19

      【2022省赛B组】试题B:顺子日期

      题解

      😄 枚举2022年所有的日期,然后判断是否存在顺子即可。

      👀️ 基本思路是:本题的顺子为顺序的三位数,包括012,123,234,345,456,567,678,789。

      🎉️ 进一步优化思路:由于是2022年的日期,顺子只可能是012和123,其余顺子不可能出现,因为其余均为非法日期。

      🚀️ 最终只需要枚举月、日,然后check一下每个日期中是否存在上述顺子即可。

      提交代码

      • 直接判断代码
      #include<bits/stdc++.h>
      void solve()
      {
          const std::array<int, 12> Day = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
          int ans = 0;
          for(int month = 1; month <= 12; month++)
          {
              int a = month / 10, b = month % 10;
              for(int day = 1; day <= Day[month - 1]; day++)
              {
                  int c = day / 10, d = day % 10;
                  if(b + 1 == c)
                  {
                      if(a + 1 == b || c + 1 == d)
                      {
                          ans++;
                      }
                  }
              }
          }
          std::cout << ans << "\n";            
          
      }
      int main()
      {
          std::ios::sync_with_stdio(false);
          std::cin.tie(0);
          int t = 1;
          while(t--)
          {
              solve();
          }
          return 0;
      }
      
      • 优化思路代码
      #include<bits/stdc++.h>
      using namespace std;
      int nums[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
      
      //数字转成字符串
      string To_string(int x)
      {
          string s;
          while(x)
              s += x % 10 + '0', x /= 10;
          reverse(s.begin(), s.end());
          return s;
      }
      
      //检查日期中是否存在012、123
      bool check(int year, int month, int day)
      {
          string s = To_string(year);
          if(month < 10)s += "0";
          s += To_string(month);
          if(day < 10)s += "0";
          s += To_string(day);
          return (s.find("123") != s.npos) || (s.find("012") != s.npos);
      }
      
      int main()
      {
          int ans = 0;
          //枚举月、日
          for(int month = 1; month <= 12; month++)
          {
              for(int day = 1; day <= nums[month]; day++)
              {
                  if(check(2022, month, day))
                      ans++;
              }
          }
          cout<<ans<<endl;
          return 0;
      }
      
      • 1

      信息

      ID
      967
      时间
      1000ms
      内存
      256MiB
      难度
      2
      标签
      递交数
      153
      已通过
      55
      上传者