为什么全是WA啊!!求大佬帮我看看!!

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

DDoinb1 @ 2021-02-02 19:45:11

#include<stdio.h>
int main()
{
    int n,i,j,k=0,c[100];
    char a[100];/*存放将要显示的数字*/ 

    scanf("%d",&n);/*数字个数*/ getchar();

    for(i=0;i<n;i++){
        a[i]=getchar();
        c[i]=a[i]-'0';
    }/*c[i]存放将要显示的数字*/ 

        for(i=0;i<5;i++){
            for(j=0;j<n;j++){
                if(i==0){
                switch(c[j]){
                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:printf("XXX");break;
                case 7:printf("XXX");break;
                case 8:printf("XXX");break;
                case 9:printf("XXX");break;

            }
            }
            if(i==1){
                switch(c[j]){
                case 0:printf("X.X");break;
                case 1:printf("..X");break;
                case 2:printf("..X");break;
                case 3:printf("..X");break;
                case 4:printf("X.X");break;
                case 5:printf("X..");break;
                case 6:printf("X..");break;
                case 7:printf("..X");break;
                case 8:printf("X.X");break;
                case 9:printf("X.X");break;

            }
            }
            if(i==2){
                    switch(c[j]){
                case 0:printf("X.X");break;
                case 1:printf("..X");break;
                case 2:printf("XXX");break;
                case 3:printf("XXX");break;
                case 4:printf("XXX");break;
                case 5:printf("XXX");break;
                case 6:printf("XXX");break;
                case 7:printf("..X");break;
                case 8:printf("XXX");break;
                case 9:printf("XXX");break;

            }
            }
            if(i==3){
                    switch(c[j]){
                case 0:printf("X.X");break;
                case 1:printf("..X");break;
                case 2:printf("X..");break;
                case 3:printf("..X");break;
                case 4:printf("..X");break;
                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;

            }
            }
            if(i==4){
                    switch(c[j]){
                case 0:printf("XXX");break;
                case 1:printf("..X");break;
                case 2:printf("XXX");break;
                case 3:printf("XXX");break;
                case 4:printf("..X");break;
                case 5:printf("XXX");break;
                case 6:printf("XXX");break;
                case 7:printf("..X");break;
                case 8:printf("XXX");break;
                case 9:printf("XXX");break;

            }
            }
            if(j!=n-1) printf(".");

        }
        printf("\n");
        }

}

by _caiji_ @ 2021-02-02 19:51:07

@DDoinb1 把

scanf("%d",&n);/*数字个数*/ getchar();

改成

scanf("%d",&n);/*数字个数*/ getchar(),getchar();

原因:数据是在windows环境造的,而windows的换行是\r\n,导致你的c[0]读成了\n,所以再加一个getchar就可以了


by _Life_ @ 2021-02-02 19:53:20

@DDoinb1 这样改

#include<stdio.h>
int main()
{
    int n,i,j,k=0/*,c[100]*/;
    char a[100];/*存放将要显示的数字*/ 
    scanf("%d %s",&n,a);/*数字个数*/

    /*for(i=0;i<n;i++){
        a[i]=getchar();
        c[i]=a[i]-'0';
    }/*c[i]存放将要显示的数字*/ 

        for(i=0;i<5;i++){
            for(j=0;j<n;j++){
                if(i==0){
                switch(a[j]-'0'){
                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:printf("XXX");break;
                case 7:printf("XXX");break;
                case 8:printf("XXX");break;
                case 9:printf("XXX");break;

            }
            }
            if(i==1){
                switch(a[j]-'0'){
                case 0:printf("X.X");break;
                case 1:printf("..X");break;
                case 2:printf("..X");break;
                case 3:printf("..X");break;
                case 4:printf("X.X");break;
                case 5:printf("X..");break;
                case 6:printf("X..");break;
                case 7:printf("..X");break;
                case 8:printf("X.X");break;
                case 9:printf("X.X");break;

            }
            }
            if(i==2){
                    switch(a[j]-'0'){
                case 0:printf("X.X");break;
                case 1:printf("..X");break;
                case 2:printf("XXX");break;
                case 3:printf("XXX");break;
                case 4:printf("XXX");break;
                case 5:printf("XXX");break;
                case 6:printf("XXX");break;
                case 7:printf("..X");break;
                case 8:printf("XXX");break;
                case 9:printf("XXX");break;

            }
            }
            if(i==3){
                    switch(a[j]-'0'){
                case 0:printf("X.X");break;
                case 1:printf("..X");break;
                case 2:printf("X..");break;
                case 3:printf("..X");break;
                case 4:printf("..X");break;
                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;

            }
            }
            if(i==4){
                    switch(a[j]-'0'){
                case 0:printf("XXX");break;
                case 1:printf("..X");break;
                case 2:printf("XXX");break;
                case 3:printf("XXX");break;
                case 4:printf("..X");break;
                case 5:printf("XXX");break;
                case 6:printf("XXX");break;
                case 7:printf("..X");break;
                case 8:printf("XXX");break;
                case 9:printf("XXX");break;

            }
            }
            if(j!=n-1) printf(".");

        }
        printf("\n");
        }

}

by _Life_ @ 2021-02-02 19:56:04

@DDoinb1 错因已经有人告诉你了,之后可以先在洛谷IDE测一下,做字符串题的时候记得要考虑全换行符的情况

我才不会告诉你洛谷的字符串题数据特别坑,全被污染过


by DDoinb1 @ 2021-02-03 09:57:56

过了过了!!!多谢大佬!!


|