运行时间要281ms,太长了,求改进建议
查看原帖
运行时间要281ms,太长了,求改进建议
1388718
Fishcake_pgm楼主2024/11/28 15:18
#include<iostream>
#include<string>
using namespace std;
int main(){
    int n,x;
    int counter = 0;
    string helper;
    cin>>n>>x;
    for (int i = 1; i<n+1; i++){
//        cout<<i<<endl;
        helper = to_string(i);
//        cout<<helper<<endl;
        for (int j = 0; j<helper.length(); j++){
            if (helper[j] - '0' == x){
                counter += 1;
//                cout<<helper<<endl;
//                cout<<counter<<endl;
            }
        }
    }
    cout<<counter<<endl;
}

2024/11/28 15:18
加载中...