全WA求助,样例也不对

B2068 统计满足条件的 4 位数

1214__qwq @ 2022-04-19 09:38:26

#include <iostream>
using namespace std;

int n, a[105][10], count = 0;
char c;

int main()
{
    cin >> n;
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= 4; j++)
        {
            c = getchar();
            a[i][j] = (int)(c - 48);
        }
        getchar();
        if(a[i][4] - a[i][1] - a[i][2] - a[i][3] > 0)
            count++;
    }
    cout << count << endl;
    return 0;
}

用字符读每一位存进二维数组里面,为啥不对


by 1214__qwq @ 2022-04-19 09:39:55

本地测试样例也不行,输出是5


by FQR_ @ 2022-04-19 09:53:56

@ryng1214 八成是读入出了问题,直接用string的cin吧。


by WilliamFranklin @ 2022-04-19 09:56:54

#include <iostream>
using namespace std;

int n, a[105][10], count = 0;
char c;

int main()
{
    cin >> n;
    getchar();
    for(int i = 1; i <= n; i++)
    {
        for(int j = 1; j <= 4; j++)
        {
            cin>>c;
            a[i][j]  = (c - 48);
        }
        getchar();
        if(a[i][4] - a[i][1] - a[i][2] - a[i][3] > 0)
            count++;
    }
    cout << count << endl;
    return 0;
}

在你基础上改了一下(尽量用cin


by 编码落寞 @ 2022-04-19 09:58:02

@ryng1214

环境不一样,第二个地方

        getchar();  
        getchar();

放两个,是可以过样例的


by 1214__qwq @ 2022-04-19 10:04:17

@WilliamFranklin 过了,谢谢


by 2024yejinghong @ 2023-08-22 17:54:14

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n,ans=0;
    cin>>n;
    for(int i=1;i<=n;i++)
    {
        int m,a,b,c,d;
        cin>>m;
        a=m%10;
        b=m/10%10;
        c=m/100%10;
        d=m/1000;
        if(a-b-c-d>0)
        {
            ans++;
        }
    }
    cout<<ans;
    return 0;
}

|