提问 / 关于 printf

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

Ender_hz @ 2020-09-04 22:12:14

这样AC/100

#include<string>
#include<iostream>
#include<cstdio>
using namespace std;
int n;
char c[101];
string p[10][5]={{"XXX","X.X","X.X","X.X","XXX"},{"..X","..X","..X","..X","..X"},{"XXX","..X","XXX","X..","XXX"},{"XXX","..X","XXX","..X","XXX"},{"X.X","X.X","XXX","..X","..X"},{"XXX","X..","XXX","..X","XXX"},{"XXX","X..","XXX","X.X","XXX"},{"XXX","..X","..X","..X","..X"},{"XXX","X.X","XXX","X.X","XXX"},{"XXX","X.X","XXX","..X","XXX"}};
int main()
{
    scanf("%d",&n);
    scanf("%s",c);
    for(int i=0;i<=4;i++)
    {
        for(int j=0;j<=n-2;j++)
            cout<<p[c[j]-'0'][i]<<".";
        cout<<p[c[n-1]-'0'][i]<<endl;
    }
}

cout换成printf就爆零

求解

好家伙是不是歧视我printf


by AdventureExtremeX @ 2020-09-04 22:12:50

你printf写的啥?


by Ender_hz @ 2020-09-04 22:14:16

cin跟scanf互换是没问题的

看错误信息好像是输出了奇怪的字符(ASCII是0或负数)


by atarashiTLE @ 2020-09-04 22:15:57

您printf用string?


by Ender_hz @ 2020-09-04 22:16:03

@xiaozengXW

            printf("%s.",p[c[j]-'0'][i]);
        printf("%s\n",p[c[n]-'0'][i]);

by Ender_hz @ 2020-09-04 22:16:27

原来使用char[]的


by Spasmodic @ 2020-09-04 22:27:20

@Ender_hz 完整代码


by WanderingTrader @ 2020-09-04 23:07:26

@Ender_hz 我看到您的代码了

printf不支持string,因为string是C++中STL里新增的容器,而printf是C语言里的


by AMIRIOX無暝 @ 2020-09-04 23:53:16

printf("%s\n",(std::string)str.c_str());

by Ender_hz @ 2020-09-05 10:03:20

谢谢大家

我重新看了一下代码,发现犯了好多低级错误

现在搞懂了


|