蓝桥杯2024年第十五届省赛真题-好数

思路:枚举所有数,每个数分别判断。代码时间复杂度虽然是n^2,但是由于判断的数长度最长是7位,用字符串处理最多只循环7次,所以最大时间复杂度小 7*10的七次方,不会超时。库中的to_string时间复杂度太大,使用会超时。

cpp 复制代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define endl '\n'

bool charge(string x){
    int k = 1;
    int len = x.size();
    for(int i=0;i<len;i++){
        if(k%2==1&&x[i]%2==1){k++;continue;}
        else if(k%2==0&&x[i]%2==0){k++;continue;}
        else return false;
    }
    return true;
}

string my_to_string(int x){
    string sx="";
    while(x){
        sx.push_back(x%10+'0');
        x/=10;
    }
    return sx;
}

void solve(){
    ios::sync_with_stdio(false),cin.tie(0),cout.tie(0);
    int n;cin>>n;
    int ans=0;
    for(int i=1;i<=n;i++){
        if(charge(my_to_string(i)))ans++;
    }
    cout<<ans<<endl;
}

signed main(){
    int T=1;
    while(T--)solve();
    return 0;
}
相关推荐
鸥梨菌Honevid5 小时前
UVM Sequencer 深度解析:FIFO 深度、req/rsp 应答模型与高频面试坑
面试·职场和发展
studyForMokey6 小时前
【Android 面试】Paging3
android·面试·职场和发展
aqiu1111117 小时前
蓝桥杯真题复盘:Fibonacci 数列与黄金分割(C++ 算法详解)
c++·算法·蓝桥杯
.道阻且长.7 小时前
Linux软件包管理器
linux·运维·ubuntu·职场和发展·centos
考研保研资料分享7 小时前
211英语专业跨保清华法学:2022年法学面试与口译考核复盘
面试·职场和发展
考研保研资料分享7 小时前
2024双非材料保研复旦:五校面试题与科研复盘
面试·职场和发展
救救孩子把16 小时前
AI面试八股文-21_21-多模态Agent面试题-Vision-Language-Agent
人工智能·面试·职场和发展·llava·多模态agent
Nil20819 小时前
leetcode 5最长回文子串
算法·leetcode·职场和发展
救救孩子把20 小时前
AI面试八股文-14_14-MCP-Skill-Function Calling
人工智能·安全·面试·职场和发展·推理·mcp·functioncall
aqiu1111111 天前
题目总结:[P8752 [蓝桥杯 2021 省 B2] 特殊年份]
蓝桥杯