player-none 发表于 2025-3-9 15:26:14

记录遇见的一道题目

本帖最后由 player-none 于 2025-3-15 12:20 编辑

https://t.tutu.to/img/CE7wQ
https://t.tutu.to/img/CElHh
https://t.tutu.to/img/CEKN6

#include <iostream>
#include <algorithm>
using namespace std;
int getdays(int y,int m){
    int a;
    if(m==1){
      a=31;
    }else if(m==2){
      if(y%400==0)a=29;
      else if(y%100==0)a=28;
      else if(y%4==0)a=29;
      else a=28;
    }else if(m==3){
      a=31;
    }else if(m==4){
      a=30;
    }else if(m==5){
      a=31;
    }else if(m==6){
      a=30;
    }else if(m==7){
      a=31;
    }else if(m==8){
      a=31;
    }else if(m==9){
      a=30;
    }else if(m==10){
      a=31;
    }else if(m==11){
      a=30;
    }else{
      a=31;
    }
    return a;
}
bool ishuiwen(int x){
    string s=to_string(x);
    string sb=s;
    reverse(s.begin(),s.end());
    return sb==s;
}
int main()
{
        int date1,date2,y,m,d,count=0;
        cin>>date1>>date2;
    y=date1/10000;
    m=date1%10000/100;
    d=date1%100;
    while(y*10000+m*100+d<=date2){
      if(ishuiwen(y*10000+m*100+d)){
            count+=1;
      }
          int a=getdays(y,m);
          if(d+1<=a){
                d+=1;
          }else if(m+1<=12){
                d=1;
                m+=1;
          }else{
                d=1;
                m=1;
                y+=1;
          }
    }
    cout<<count;
        return 0;
}

其实题目还简单,就是代码量大,敲着累……不过我认为这段代码非常具有……可以让我好好记录一番的特点。

FishC 发表于 2025-3-9 15:26:27

为了帮助您正确理解和解决代码中的问题,您可以参考以下这段基于您提供代码的描述:

题目分析

[*]给定两个日期,要求查找之间日期是否有回文日期。
[*]回文日期是指日期如20211202,其正读与反读相同。


代码改写建议
为了您可以轻松复制并修改代码,这里对该程序做了一定的调整。此外,请确保您在测试环境中配置了必要的头文件`<string>`和`<algorithm>`:


#include <iostream>
#include <string>
#include <algorithm> // for std::reverse
using namespace std;

int getdays(int y, int m) {
    int a;
    if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
      a = 31;
    } else if (m == 4 || m == 6 || m == 9 || m == 11) {
      a = 30;
    } else { // m == 2
      if (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0))
            a = 29;
      else
            a = 28;
    }
    return a;
}

bool ishuiwen(int x) {
    string s = to_string(x);
    string sb = s;
    reverse(s.begin(), s.end());
    return sb == s;
}

int main() {
    int date1, date2, y, m, d, count = 0;
    cin >> date1 >> date2;
    y = date1 / 10000;
    m = (date1 % 10000) / 100;
    d = date1 % 100;
    while (y * 10000 + m * 100 + d <= date2) {
      int currentDate = y * 10000 + m * 100 + d;
      if (ishuiwen(currentDate)) {
            count++;
            cout << currentDate << " is a huiwen date." << endl;
      }
      // increment date
      d++;
      if (d > getdays(y, m)) {
            d = 1;
            m++;
            if (m > 12) {
                m = 1;
                y++;
            }
      }
    }
    cout << "Total huiwen dates: " << count << endl;
}


请确保在输入日期时用符合格式的整数形式,例如:20210101。您可以通过增加调试信息(如输出每次循环的日期)来进一步验证程序的正确性。

以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页: [1]
查看完整版本: 记录遇见的一道题目