大佬们,代码运行不了,实在找不到问题在哪了!!

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

sing11 @ 2023-07-14 16:47:57

#include<iostream>
using namespace std;
main()
{
    int n,k,i,j;
    int a[101];
    string ans[5];
    scanf("%d",&n);
    for(i=0;i<n;i++) scanf("%d",&a[i]);//数字 

    k=0;
    while(k<n){
        switch(a[k]){
            case 0:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X.X";ans[2] += "X.X";ans[3] += "X.X";break;
            case 1:ans[0] += "..X";ans[4] += "..X";
            ans[1] += "..X";ans[2] += "..X";ans[3] += "..X";break;
            case 2:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "..X";ans[2] += "XXX";ans[3] += "X..";break;
            case 3: ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "..X";ans[2] += "XXX";ans[3] += "..X";break;
            case 4:ans[0] += "X.X";ans[4] += "..X";
            ans[1] += "X.X";ans[2] += "XXX";ans[3] += "..X";break;
            case 5: ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X..";ans[2] += "XXX";ans[3] += "..X";break;
            case 6: ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X..";ans[2] += "XXX";ans[3] += "X.X";break;
            case 7: ans[0] += "XXX";ans[4] += "..X";
            ans[1] += "..X";ans[2] += "..X";ans[3] += "..X";break;
            case 8:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X.X";ans[2] += "XXX";ans[3] += "X.X";break;
            case 9:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X.X";ans[2] += "XXX";ans[3] += "..X";break;  
        }           

        k++;
    }
    for(i=0;i<n;i++){
        cout<<ans[i];
        printf("\n");
    }
}

/*
字符串数组 
s[] 
*/

by qinshi0308 @ 2023-07-14 16:49:54

咋就运行不了了?


by sing11 @ 2023-07-14 16:56:24

@qinshi0308 就是输入数据然后就没有结果,感觉一直在循环...


by Register_int @ 2023-07-14 16:57:06

@sing11

#include<iostream>
using namespace std;
int n,k,i,j;
char a[101];
string ans[5];
int main()
{
    scanf("%d%s",&n,a);
    for(i=0;i<n;i++) a[i]-='0';

    k=0;
    while(k<n){
        switch(a[k]){
            case 0:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X.X";ans[2] += "X.X";ans[3] += "X.X";break;
            case 1:ans[0] += "..X";ans[4] += "..X";
            ans[1] += "..X";ans[2] += "..X";ans[3] += "..X";break;
            case 2:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "..X";ans[2] += "XXX";ans[3] += "X..";break;
            case 3: ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "..X";ans[2] += "XXX";ans[3] += "..X";break;
            case 4:ans[0] += "X.X";ans[4] += "..X";
            ans[1] += "X.X";ans[2] += "XXX";ans[3] += "..X";break;
            case 5: ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X..";ans[2] += "XXX";ans[3] += "..X";break;
            case 6: ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X..";ans[2] += "XXX";ans[3] += "X.X";break;
            case 7: ans[0] += "XXX";ans[4] += "..X";
            ans[1] += "..X";ans[2] += "..X";ans[3] += "..X";break;
            case 8:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X.X";ans[2] += "XXX";ans[3] += "X.X";break;
            case 9:ans[0] += "XXX";ans[4] += "XXX";
            ans[1] += "X.X";ans[2] += "XXX";ans[3] += "..X";break;  
        }           
        if (k + 1 < n) for (i = 0; i < 5; i++) ans[i] += ".";
        k++;
    }
    for(i=0;i<5;i++){
        cout<<ans[i];
        printf("\n");
    }
}

by dsfgsdf @ 2023-07-14 17:00:36

@sing11 输入的时候,字符串部分需要一个字符隔一个空格输入,你试一下,肯定是程序编的有问题


by dsfgsdf @ 2023-07-14 17:01:29

@sing11 需要这样输入:

10 0 1 2 3 4 5 6 7 8 9


by dsfgsdf @ 2023-07-14 17:02:05

@sing11 提醒:最好在main前面加上int


by dsfgsdf @ 2023-07-14 17:03:51

@lfxxzms wait,重发


by dsfgsdf @ 2023-07-14 17:04:28

你的程序输出:

XXX..XXXXXXXX.XXXXXXXXXXXXXXXX
X.X..X..X..XX.XX..X....XX.XX.X
X.X..XXXXXXXXXXXXXXXX..XXXXXXX
X.X..XX....X..X..XX.X..XX.X..X
XXX..XXXXXXX..XXXXXXX..XXXXXXX

by LiZeheng16 @ 2023-07-14 17:06:50

@sing11 打个表嘛


by sing11 @ 2023-07-14 17:22:14

@lfxxzms 噢!题目给的是没有空格字符串,我明白了。谢谢!!


| 下一页