有什么办法能够做到横向输出呢?

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

fengfenglong @ 2022-09-12 16:45:06

#include <iostream>
#include <iomanip>
#include <stdio.h>
#include <bits/stdc++.h>
#include <math.h>
#include <string>
#include <cstring>
using namespace std;
//#define PI 3.1415926
void xianshi(char n)
{
    char a[10][10];
    if(n == '0')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = 'X';a[2][1] = '.';a[2][2] = 'X';
        a[3][0] = 'X';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '1')
    {
        a[0][0] = '.';a[0][1] = '.';a[0][2] = 'X';
        a[1][0] = '.';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = '.';a[2][1] = '.';a[2][2] = 'X';
        a[3][0] = '.';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = '.';a[4][1] = '.';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '2')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = '.';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = 'X';a[2][1] = 'X';a[2][2] = 'X';
        a[3][0] = 'X';a[3][1] = '.';a[3][2] = '.';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '3')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = 'X';a[2][1] = '.';a[2][2] = 'X';
        a[3][0] = 'X';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '4')
    {
        a[0][0] = 'X';a[0][1] = '.';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = 'X';a[2][1] = 'X';a[2][2] = 'X';
        a[3][0] = '.';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = '.';a[4][1] = '.';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '5')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = '.';
        a[2][0] = 'X';a[2][1] = 'X';a[2][2] = 'X';
        a[3][0] = '.';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '6')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = '.';
        a[2][0] = 'X';a[2][1] = 'X';a[2][2] = 'X';
        a[3][0] = 'X';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '7')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = '.';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = '.';a[2][1] = '.';a[2][2] = 'X';
        a[3][0] = '.';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = '.';a[4][1] = '.';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '8')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = 'X';a[2][1] = 'X';a[2][2] = 'X';
        a[3][0] = 'X';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
    else if(n == '9')
    {
        a[0][0] = 'X';a[0][1] = 'X';a[0][2] = 'X';
        a[1][0] = 'X';a[1][1] = '.';a[1][2] = 'X';
        a[2][0] = 'X';a[2][1] = 'X';a[2][2] = 'X';
        a[3][0] = '.';a[3][1] = '.';a[3][2] = 'X';
        a[4][0] = 'X';a[4][1] = 'X';a[4][2] = 'X';
        for(int i = 0;i < 5;i++)
        {
            for(int j = 0;j < 3;j++)
            {
                cout << a[i][j];
            }
            if(i != 4)
            cout << endl;
        }
    }
}
void dian()
{
char b[10][10];
b[0][1] = '.';
b[1][1] = '.';
b[2][1] = '.';
b[3][1] = '.';
b[4][1] = '.';
for(int i = 0;i < 5;i++)
cout << b[i][1] <<endl;
}
int main()
{
int n;
cin >> n;
char c[110];
for(int i = 1;i  <= n ;i++)
{
cin >> c[i];
xianshi(c[i]);
if(i != n)
dian();
}
return 0;
}

我写的这个代码只能做到竖向的输出有什么办法能够做到在输出第一个数字之后再回到第一行继续输出下一个数字。


by ud2_ @ 2022-09-12 16:58:45

输出之后就回不去了,要在输出第二行之前把所有数字的第一行写完。


by zpw_bth @ 2022-09-12 17:01:26

@fengfenglong 这道题就是考验你能不能想出能够形成竖向输出的效果的代码


|