30求助

P2010 [NOIP2016 普及组] 回文日期

maxlmb2023 @ 2023-10-02 13:27:31

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int schw(int x)
{

    int a = x % 10;
    x /= 10;
    int b = x % 10;
    x /= 10;
    int c = x % 10;
    x /= 10;
    int d = x % 10;
    x /= 10;
    int ans = 0;
    ans = ans * 10 + a;
    ans = ans * 10 + b;
    ans = ans * 10 + c;
    ans = ans * 10 + d;
    ans = ans * 10 + d;
    ans = ans * 10 + c;
    ans = ans * 10 + b;
    ans = ans * 10 + a;
    return ans;
}
const int D[] = {0,31,29,31,30,31,30,31,31,30,31,30,31};
int a, b, aa, bb, ans = 0;
bool IsDate(int x)
{
    int y, m, d;
    y = x / 10000;
    m = x / 100 % 100;
    d = x % 100;
    if(m <= 0 || m >= 13) return 0;
    if(D[m] != d) return 0;
    return 1;

}
int main()
{

    cin >> a>> b;
    aa = a / 10000;
    bb = b / 10000;
    for(int i = aa; i <= bb; i++)
    {
        int j = schw(i);
        if(IsDate(j) && a <= j && j <= b) ans++;
    }
    cout << ans;
    return 0;
}

30分,wa的输出比标准答案小。


by danzai10 @ 2023-10-05 21:13:04


#include<bits/stdc++.h>
using namespace std;
int month[15]={0,31,29,31,30,31,30,31,31,30,31,30,31};
int x,y,sum;
int main(){
  cin>>x>>y;
  for(int i=1;i<=12;i++){
      for(int j=1;j<=month[i];j++){
        int k=j%10*1000+(j/10)*100+(i%10)*10+(i/10);
        k=k*10000+i*100+j;
        if(k>=x&&k<=y){
          sum++;
        }
      }
  }
  cout<<sum;
  return 0;
}

|