求助 为什么把scanf输入换成cin就没事了

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

RizzzzziR @ 2021-06-18 16:40:24

以下为原来的代码,问题主要出在输入部分

#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
using namespace std;
int n;
int opn[105];
char ch;
char num[10][5][3]={
{{'X','X','X'},
{'X','.','X'},
{'X','.','X'},
{'X','.','X'},
{'X','X','X'},},//0
{{'.','.','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},},//1
{{'X','X','X'},
{'.','.','X'},
{'X','X','X'},
{'X','.','.'},
{'X','X','X'},},//2
{{'X','X','X'},
{'.','.','X'},
{'X','X','X'},
{'.','.','X'},
{'X','X','X'},},//3
{{'X','.','X'},
{'X','.','X'},
{'X','X','X'},
{'.','.','X'},
{'.','.','X'},},//4
{{'X','X','X'},
{'X','.','.'},
{'X','X','X'},
{'.','.','X'},
{'X','X','X'},},//5
{{'X','X','X'},
{'X','.','.'},
{'X','X','X'},
{'X','.','X'},
{'X','X','X'},},//6
{{'X','X','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},},//7
{{'X','X','X'},
{'X','.','X'},
{'X','X','X'},
{'X','.','X'},
{'X','X','X'},},//8
{{'X','X','X'},
{'X','.','X'},
{'X','X','X'},
{'.','.','X'},
{'X','X','X'},}};//9

int main()
{
    scanf("%d%c",&n,&ch);
    for(int i=1;i<=n;i++)
    {
        scanf("%c",&ch);
        opn[i]=ch-'0';
    }
    for(int i=0;i<5;i++)
        for(int j=1;j<=n;j++)
        {
            for(int k=0;k<3;k++)
                cout<<num[opn[j]][i][k];
            if(j!=n)    cout<<".";
                else    cout<<endl;
        }
    return 0;
}

都出现了相同的问题: Wrong Answer. wrong answer On line 1 column 1, read (ASCII 0), expected ..

然后仅把输入部分换成:

    cin>>n;
    for(int i=1;i<=n;i++)
    {
        cin>>ch;
        opn[i]=ch-'0';
    }

就没事(AC)了,想请教一下原因


by RizzzzziR @ 2021-06-18 16:43:33

对了,这两种输入方式在我自己的电脑上测试样例和下载的第一个测试点时都没有问题


by XYY1411 @ 2021-06-18 16:48:42

Windows 和 Linux 不一样, Windows 换行符是 \r\n, Linux 是 \n


by E1_de5truct0r @ 2021-06-18 17:03:25

@shcl_jjjrzly

scanf("%d%c",&n,&ch) 改成 scanf("%d",&n) ,然后 scanf("%c",&ch) 改成 scanf(" %c",&ch) 应该就好了。


by E1_de5truct0r @ 2021-06-18 17:03:56

@shcl_jjjrzly

#include<iostream>
#include<cstdio>
#include<string>
#include<cctype>
using namespace std;
int n;
int opn[105];
char ch;
char num[10][5][3]={
{{'X','X','X'},
{'X','.','X'},
{'X','.','X'},
{'X','.','X'},
{'X','X','X'},},//0
{{'.','.','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},},//1
{{'X','X','X'},
{'.','.','X'},
{'X','X','X'},
{'X','.','.'},
{'X','X','X'},},//2
{{'X','X','X'},
{'.','.','X'},
{'X','X','X'},
{'.','.','X'},
{'X','X','X'},},//3
{{'X','.','X'},
{'X','.','X'},
{'X','X','X'},
{'.','.','X'},
{'.','.','X'},},//4
{{'X','X','X'},
{'X','.','.'},
{'X','X','X'},
{'.','.','X'},
{'X','X','X'},},//5
{{'X','X','X'},
{'X','.','.'},
{'X','X','X'},
{'X','.','X'},
{'X','X','X'},},//6
{{'X','X','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},
{'.','.','X'},},//7
{{'X','X','X'},
{'X','.','X'},
{'X','X','X'},
{'X','.','X'},
{'X','X','X'},},//8
{{'X','X','X'},
{'X','.','X'},
{'X','X','X'},
{'.','.','X'},
{'X','X','X'},}};//9

int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        scanf(" %c",&ch);
        opn[i]=ch-'0';
    }
    for(int i=0;i<5;i++)
        for(int j=1;j<=n;j++)
        {
            for(int k=0;k<3;k++)
                cout<<num[opn[j]][i][k];
            if(j!=n)    cout<<".";
                else    cout<<endl;
        }
    return 0;
}

by A4869 @ 2021-06-18 17:37:26

scanf数后面读取字符会读成' ',cin自动跳过' ',所以没事


by RizzzzziR @ 2021-06-19 18:23:22

@zhengjinchen @wwwyq07 确实是这样,非常感谢


|