P10424 [蓝桥杯 2024 省 B] 好数

· · 个人记录

错误原因

由于在字符串前面添加字符是 o(n) 的,所以超时了。

思路

按照题目意思模拟即可。

代码

#include<bits/stdc++.h>
#define int long long
using namespace std;
int n;
bool check(int x){
    int j=1;
    while(x){
        int v=x%10;
        if((v%2)!=(j%2))return false;
        j++;
        x/=10;
    }
    return true;
}
int ans;
signed main(){
    ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;i++){
        if(check(i)){
            ans++;
        }
    }
    cout<<ans;
    return 0;
}