4.82s超时求助

B2078 含 k 个 3 的数

Qf_fd @ 2024-10-09 18:21:02

code

#include <stdio.h>

int main()
{
    long long m;
    int k;
    scanf("%lld %d", &m, &k);
    int cnt = 1, cnt1 = 0, cnt2 = 0;
    // cnt1为m的位数
    for (int i = 1; i < m; i *= 10)
    {
        cnt *= 10;
        cnt1++;
    }
    cnt /= 10;
    for (int i = 0; i < cnt1; i++)
    {
        int x = m / cnt;
        m = m % cnt;
        cnt /= 10;
        if (x == 3)
        {
            cnt2++;
        }
    }
    if (cnt2 == k)
    {
        printf("YES");
    }else{
        printf("NO");
    }

    return 0;
}

by Sheep_YCH @ 2024-10-09 18:26:46

@Qf_fd

这道题用字符串或者字符数组就行


by bladrrxy @ 2024-10-09 18:42:15

for (int i = 1; i < m; i *= 10)


by THE_SUPER_WING @ 2024-10-09 18:59:42

#include <bits/stdc++.h>

using namespace std;

int main()
{
    long long m; //开long long
    int k, ans = 0;
    cin >> m >> k;
    while(m != 0)
    {
        int g = m % 10;//一位一位看
        if(g == 3) ans++;//判断是否为3,是就ans++
        m /= 10;
    } 
    if(ans == k) cout << "YES";
    else cout << "NO"; 
    return 0;
}

求关注


by Qf_fd @ 2024-10-09 20:39:50

@bladrrxy 谢谢,已解决

#include <stdio.h>

int main()
{
    long long m;
    int k;
    scanf("%lld %d", &m, &k);
    int cnt2 = 0;
    while(m>0){
        int n = m % 10;
        if(n==3){
            cnt2++;
        }
        m /= 10;
    }    
    if (cnt2 == k){
        printf("YES");
    }
    else{
        printf("NO");
    }

    return 0;
}

|