关于我又一次觉得我应该全对但是全错这件事

P5730 【深基5.例10】显示屏

SHI250011 @ 2022-10-08 17:10:00

弱狗求求大佬帮帮我吧,为什么我答案明明跟示例一模一样还是错啊。。。。我真的快坚持不下去了。呜呜呜


#include<iostream>
using namespace std;
char a[6][400];
char b[101];
int main() {
    int n = 0; cin >> n;
    int c = ((n * 3) + n - 1);//一共有多少行暗点
    for (int i = 1; i <= 5; i++)
        for (int j = 1; j <= c; j++)
            a[i][j] = '.';//设置所有暗点

    for (int i = 0; i <= n; i++) {//把自然数以字符形式存入数组b
        b[i] = getchar();
    }
    int h = 1;
    for (int t = 1; t <= n; t++) {//根据数组b打印对应数字
        char i = b[t];
        int  l = 1;
        if (i == '0') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h] = a[l + 1][h + 2] = a[l + 2][h] = a[l + 2][h + 2] = a[l + 3][h] = a[l + 3][h + 2] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
                h += 4;
        }
        else if (i == '1') {
            a[l][h + 2] = a[l + 1][h + 2] = a[l + 2][h + 2] = a[l + 3][h + 2] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '2') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h + 2] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '3') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h + 2] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h + 2] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '4') {
            a[l][h] = a[l][h + 2] = a[l + 1][h] = a[l + 1][h + 2] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h + 2] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '5') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h + 2] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '6') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h] = a[l + 3][h + 2] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '7') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h + 2] = a[l + 2][h + 2] = a[l + 3][h + 2] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '8') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h] = a[l + 1][h + 2] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h] = a[l + 3][h + 2] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
        else if (i == '9') {
            a[l][h] = a[l][h + 1] = a[l][h + 2] = a[l + 1][h] = a[l + 1][h + 2] = a[l + 2][h] = a[l + 2][h + 1] = a[l + 2][h + 2] = a[l + 3][h + 2] = a[l + 4][h] = a[l + 4][h + 1] = a[l + 4][h + 2] = 'X';
            h += 4;
        }
    }
    for (int i = 1; i <= 5; i++) {
        for (int j = 1; j <= c; j++) {
            cout << a[i][j];
        }
        cout << "\n";
    }
    return 0;
}

by jnyz2021109122116 @ 2022-10-08 17:14:17

@SHI250011 老生常谈的问题了,在读第二行之前要加getchar


by _l_l_ @ 2022-10-08 17:26:22

@SHI250011 洛谷使用 Linux 环境测试,因此每行后结尾的字符不同,不建议单纯的使用 getchar

建议将 b[i] = getchar(); 改为 while ((b[i] = getchar()) < '0');,将这句话前面的 for 循环改回 1 ~ n。


by SHI250011 @ 2022-10-08 17:59:41

@jnyz2021109122116 多谢大佬Orz


by SHI250011 @ 2022-10-08 18:00:22

@_ll 多谢大佬Orz


|