为什么会有错

B2078 含 k 个 3 的数

Hongyang66666 @ 2023-12-16 19:55:15

#include <stdio.h>
#include <math.h>
int main()
{
    long long m;
    int k,count,t=0;
    scanf("%lld %d", &m, &k);
    while(m>0){
        t=m%10;
        if(t==3){
            count++;
        }
        m=m/10;
    }
    if(count==k){
        printf("YES");
    }
    else{
        printf("NO");
    }
    return 0;
}

by cff_0102 @ 2023-12-16 20:01:28

@Hongyang66666 count初始化一下


by Hongyang66666 @ 2023-12-16 20:10:17

@cff_0102 哦哦这样就对了,谢啦


by chen__pengyu @ 2023-12-17 15:51:05

#include<iostream>
#include<cstdio>
using namespace std;
int main() {
    long long m,k=0;
    int g,a;
    scanf("%lld",&m);
    cin>>g;
    while(m) {
        a=m%10;
        if(a==3) {
            k++;
        }
        m=m/10;
    }
    if(k==g){
        printf("YES");
    }else{
        printf("NO");
    }
    return 0;
}

这样就OK了


by Kuang_He @ 2023-12-31 20:35:10

#include<bits/stdc++.h>
using namespace std;int main() {char n;int k, cnt = 0;while ((n = getchar()) != ' ') if (n == '3')cnt++;cin >> k; if (cnt == k) cout << "YES";else cout << "NO"; return 0;}

|