70,蒟蒻求助!

B2078 含 k 个 3 的数

HYrwb @ 2023-08-18 15:42:59

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

by Gohldg @ 2023-08-18 15:58:03

@HYrwb 话不多说上代码:

#include <iostream>
using namespace std;
int main(){
    long long sum=0,k,m;
    cin>>m>>k;
    while(m>=1){
        if(m%10==3){sum++;}
        m/=10;
    }
    if(sum==k){
        cout <<"YES";
    }else if(sum!=k){
        cout <<"NO";
    }
}

by fuyanchun @ 2023-08-18 15:58:28

正解:用to_string函数把int转string再判断有没有k个3

正解中的正解:送1关


by myyyIisq2R @ 2023-08-18 15:59:51

@HYrwb 用long long了但没有完全用long long

#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
using namespace std;
#define int long long
int pd(int m)
{
    int cnt=0;
    int s;
    while(m)
    {
        s = m%10;
        m/=10;
        if(s==3)
        {
            cnt++;
        }
    }
    return cnt;
}
signed main()
{
long long m,k;
cin>> m >>k; 
if(pd(m)==k)
{
    cout <<"YES";
}
else 
{
    cout <<"NO";
}
return 0;
}

by myyyIisq2R @ 2023-08-18 16:00:55

@HYrwb 我的

#include <bits/stdc++.h>
using namespace std;
int main()
{
    string a;
    int b;
    cin>>a>>b;
    int cnt;
    for(int i{};i<a.length();i++)
    {
        if(a[i] =='3') cnt++;
    }
    if(cnt == b) cout<<"YES\n";
    else cout<<"NO";
}

by fuyanchun @ 2023-08-18 16:01:06

@HYrwb Code:

#include<bits/stdc++.h>
using namespace std;
string s;
int k;
int main(){
    cin>>s>>k;
    if(count(s.rbegin(),s.rend(),'3')==k)cout<<"YES";
    else cout<<"NO";
    return 0;
}

by HYrwb @ 2023-08-18 16:11:08

感谢三位大佬解答,AC了~~~~


|