本地运行结果与洛谷IDE不同,求指点

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

yixiuge1 @ 2021-11-24 00:11:09

此代码在vs上运行时只需要一个getchar()来读取n和数组之间的回车(我也这么认为)。但在洛谷上提交需要两个getchar();否则x【0】就是-38.

#include<stdio.h>
int main() {
    int n;
    char c;
    int x[100] = { 0 };
    int count = 0;
    scanf("%d", &n);
    getchar();//读取回车
    getchar();

    for (int i = 0; i < n; i++) {
        x[i] = getchar() - 48;
    }//输入

    for (int i = 0; i < n; i++) {//每一行都循环一遍输入,共五遍
        switch (x[i]) {
        case 0:printf("XXX"); break;
        case 1:printf("..X"); break;
        case 2:printf("XXX"); break;
        case 3:printf("XXX"); break;
        case 4:printf("X.X"); break;
        case 5:printf("XXX"); break;
        case 6:
        case 7:
        case 8:
        case 9:printf("XXX"); break;
        default:printf("p"); break;
        }
        count++;
        if (count != n) printf(".");//最后一项不要点

    }printf("\n"); count = 0;
    for (int i = 0; i < n; i++) {
        switch (x[i]) {
        case 0:printf("X.X"); break;
        case 1:
        case 2:
        case 3:printf("..X"); break;
        case 4:printf("X.X"); break;
        case 5:
        case 6:printf("X.."); break;
        case 7:printf("..X"); break;
        case 8:
        case 9:printf("X.X"); break;
        }
        count++;
        if (count != n) printf(".");

    }printf("\n"); count = 0;
    for (int i = 0; i < n; i++) {
        switch (x[i]) {
        case 0:printf("X.X"); break;
        case 1:printf("..X"); break;
        case 2:
        case 3:
        case 4:
        case 5:
        case 6:printf("XXX"); break;
        case 7:printf("..X"); break;
        case 8:
        case 9:printf("XXX"); break;
        }
        count++;
        if (count != n) printf(".");

    }printf("\n"); count = 0;
    for (int i = 0; i < n; i++) {
        switch (x[i]) {
        case 0:printf("X.X"); break;
        case 1:printf("..X"); break;
        case 2:printf("X.."); break;
        case 3:
        case 4:
        case 5:printf("..X"); break;
        case 6:printf("X.X"); break;
        case 7:printf("..X"); break;
        case 8:printf("X.X"); break;
        case 9:printf("..X"); break;
        }
        count++;
        if (count != n) printf(".");

    }printf("\n"); count = 0;
    for (int i = 0; i < n; i++) {
        switch (x[i]) {
        case 0:printf("XXX"); break;
        case 1:printf("..X"); break;
        case 2:printf("XXX"); break;
        case 3:printf("XXX"); break;
        case 7:
        case 4:printf("..X"); break;
        case 5:
        case 6:
        case 8:
        case 9:printf("XXX"); break;
        }
        count++;
        if (count != n) printf(".");

    } 

    return 0;
}

这是提交全过的代码。 这是一个getchar()的运行结果(在vs上这才是正确的) p.XXX...X.XXX.XXX.X.X.XXX.XXX.XXX.XXX .X.X...X...X...X.X.X.X...X.....X.X.X .X.X...X.XXX.XXX.XXX.XXX.XXX...X.XXX .X.X...X.X.....X...X...X.X.X...X.X.X .XXX...X.XXX.XXX...X.XXX.XXX...X.XXX

(p是第一个switch——case的default) 求指点。


by CGDGAD @ 2021-11-24 07:02:55

确实是这样的。洛谷数据在 Windows 里造,换行符是 \r\n,在 win 下运行没事,因为 linux 换行符只有一个 \n 所以会把它当成两个字符


by yixiuge1 @ 2021-11-24 13:41:26

@AFObject 谢谢大佬解惑。我在书上看到\n只是回车,\r才是换行。但我一直用\n就实现换行加回车了,原来是环境的问题。


by JvRuoDiu @ 2022-05-23 20:56:04

@AFObject 感谢大佬提醒,才注意到这问题

#include<iostream>
using namespace std;
const int MAX_COUNT=10;
const int MAX_COL=4;
const int MAX_ROW=5;
const int MAX_INPUT=1e2+10;

char buff[MAX_INPUT];

int main()
{
   char base[MAX_COUNT][MAX_ROW][MAX_COL]={{
        {'X','X','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','X','X','\0'}
    },
    {
        {'.','.','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'X','X','X','\0'}
        ,{'X','.','.','\0'}
        ,{'X','X','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'X','X','X','\0'}
    },
    {
        {'X','.','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'X','.','.','\0'}
        ,{'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'X','X','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'X','.','.','\0'}
        ,{'X','X','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','X','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
        ,{'.','.','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','X','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','X','X','\0'}
    },
    {
        {'X','X','X','\0'}
        ,{'X','.','X','\0'}
        ,{'X','X','X','\0'}
        ,{'.','.','X','\0'}
        ,{'X','X','X','\0'}
    }};
    int count;
    cin>>count;
    cin.get();
    cin.get();//注意这里要用两个,否则就会因为win的数据的换行在Linux下漏获取一个字符,导致win下正确,Linux下错误,但是两个以后Linux下的win数据读取正确,win下就会错误,目前除了重复判断我还没有什么特别简单的办法,轻松解决这种win数据在两种系统下读取输入流时的兼容性问题,欢迎各位大佬分享
    cin.getline(buff,MAX_INPUT,'\n');
    for(int i=0;i<5;i++)
    {
        for(int j=0;j<count;j++)
        {
            cout<<base[buff[j]-'0'][i];
            if(j!=count-1)
                cout<<'.';
        }
        cout<<endl;
    }
    return 0;
}

|