超时求助!

B2078 含 k 个 3 的数

CoderMC @ 2025-01-07 17:49:54

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n;
    int k,count=0;
    cin>>n>>k;
    while(n){
        if(n%10==3){
            count++;
            n=n/10;
        }
    }
    if(count==k){
        cout<<"YES";
    }
    else cout<<"NO";
    return 0;

}

by liuzhuoran141516 @ 2025-01-07 18:06:32

#include<bits/stdc++.h>
using namespace std;
int main(){
    long long n;
    int k,count=0;
    cin>>n>>k;
    while(n){
        if(n%10==3){
            count++;  
        }
        n=n/10;
    }
    if(count==k){
        cout<<"YES";
    }
    else cout<<"NO";
    return 0;

}

@SCAU


by Baoqiancheng11 @ 2025-01-07 18:11:53

while(n){
    if(n%10==3){
        count++;
        n=n/10;
    }
}

此时if不执行时会死循环,应把n=n/10挪到if外,这样如果个位不是3,会把个位过下去看十位


by CoderMC @ 2025-01-07 19:23:04

@Baoqiancheng11thanks!


by CoderMC @ 2025-01-07 19:23:29

@liuzhuoran141516thanks!!


|