首页 > 其他分享 >P5730

P5730

时间:2022-10-13 21:14:39浏览次数:36  
标签:... P5730 .. XXX break length output

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

一道相当具有代表性的题目的解析

题面

【深基 5.例 10】显示屏

题目描述

液晶屏上,每个阿拉伯数字都是可以显示成 \(3\times5\) 的点阵的(其中 X 表示亮点,. 表示暗点)。现在给出数字位数(不超过 \(100\))和一串数字,要求输出这些数字在显示屏上的效果。数字的显示方式如同样例输出,注意每个数字之间都有一列间隔。

输入格式

第一行输入一个正整数 \(n\),表示数字的位数。

第二行输入一个长度为 \(n\) 的自然数。

输出格式

输出五行,表示显示屏上的数字。

样例 #1

样例输入 #1
10
0123456789
样例输出 #1
XXX...X.XXX.XXX.X.X.XXX.XXX.XXX.XXX.XXX
X.X...X...X...X.X.X.X...X.....X.X.X.X.X
X.X...X.XXX.XXX.XXX.XXX.XXX...X.XXX.XXX
X.X...X.X.....X...X...X.X.X...X.X.X...X
XXX...X.XXX.XXX...X.XXX.XXX...X.XXX.XXX

提示

数据保证,\(1 \leq n \leq 100\)。

思路

  1. 这题的精髓在于时间的把控和输出格式的调整
  2. 题目中要求每个数字之间都有一列间隔,那间隔是放在数字前面还是后面?如何解决端点问题?
  3. 题目的读入是一串数字,如何进行拆分

读入

    string in;
    cin >> in;
    for (int i = 0; i < in.length(); i++)
    {
        int id = in[i];
        add(id - '0');
    }

读入一个字符串in,遍历其中的每个元素,依次-'0'使其从字符变为数字。

这里用到了一个特性:C++String类实际上是一个字符数组,其中的每个元素都是一个char

    string str = "String";
    cout << typeid(str[0]).name() << '\t' << sizeof(str[0]) << endl;
//输出
    c   1
    ^   ^
    |   |
 char 占用的空间

数据对应字符串

常见的将数字和一个变量对应上的方法包括map,switch和数组。因为他是一个五行的文本,不太方便用直接输出,这里我们维护一个string数组output[5]来模拟要输出的内容,每一个需要输出的子串只要+=到源字符串上就行了。

    case 1:
        output[0] += "..X.";
        output[1] += "..X.";
        output[2] += "..X.";
        output[3] += "..X.";
        output[4] += "..X.";
        break;

这里我是在每个数字后面加了题目要求的分隔符。

处理端点

既然我是在每个数字后面添加分隔符,那只要把最后一个字符不输出就行了对吧,于是我们写

output[0][output[0]-1]=''

然后编译器告诉我们:

error: empty character constant
     output[0][output[0].length() - 1] = '';

string元素中不能有空元素……

好吧我们改一下

output[0][output[0].length() - 1] = ' ';

上洛谷,WA了。

原因其实很简单:

Presentation Error 没有

缩略语 英文全称 中文全称
OJ Online Judge 在线判题系统
AC Accepted 通过
WA Wrong Answer 答案错误
TLE Time Limit Exceed 超时
OLE Output Limit Exceed 超过输出限制
MLE Memory Limit Exceed 超出内存限制
PE Presentation Error 格式错误
RE Runtime Error 运行出错
CE Compile Error 编译错误

因为你谷没有PE这种错法

其实很明朗了,你的输出多了一个空格,所以我们有一个比较莽的做法:

for (int i = 0; i < output[0].length() - 1; i++){
    cout << output[0][i];
}

其实有更简洁的办法,在刚学字符数组char[]的时候老师会告诉你字符串的结尾字符是'\0',所以:

output[1][output[1].length() - 1] = '\0';

搞定。

效果展示

在上AC代码之前我们可以先看看这个程序的输出如何:

10
2147483648

XXX...X.X.X.XXX.X.X.XXX.XXX.XXX.X.X.XXX
..X...X.X.X...X.X.X.X.X...X.X...X.X.X.X
XXX...X.XXX...X.XXX.XXX.XXX.XXX.XXX.XXX
X.....X...X...X...X.X.X...X.X.X...X.X.X
XXX...X...X...X...X.XXX.XXX.XXX...X.XXX

如果我们把.换成空格

10
2147483648

XXX   X X X XXX X X XXX XXX XXX X X XXX
  X   X X X   X X X X X   X X   X X X X
XXX   X XXX   X XXX XXX XXX XXX XXX XXX
X     X   X   X   X X X   X X X   X X X
XXX   X   X   X   X XXX XXX XXX   X XXX

AC Code


#include <bits/stdc++.h>
using namespace std;

string output[5];

void add(int t)
{
    switch (t)
    {
        {
        case 1:
            output[0] += "..X.";
            output[1] += "..X.";
            output[2] += "..X.";
            output[3] += "..X.";
            output[4] += "..X.";
            break;
        case 2:
            output[0] += "XXX.";
            output[1] += "..X.";
            output[2] += "XXX.";
            output[3] += "X...";
            output[4] += "XXX.";
            break;

        case 3:
            output[0] += "XXX.";
            output[1] += "..X.";
            output[2] += "XXX.";
            output[3] += "..X.";
            output[4] += "XXX.";
            break;

        case 4:
            output[0] += "X.X.";
            output[1] += "X.X.";
            output[2] += "XXX.";
            output[3] += "..X.";
            output[4] += "..X.";
            break;

        case 5:
            output[0] += "XXX.";
            output[1] += "X...";
            output[2] += "XXX.";
            output[3] += "..X.";
            output[4] += "XXX.";
            break;

        case 6:
            output[0] += "XXX.";
            output[1] += "X...";
            output[2] += "XXX.";
            output[3] += "X.X.";
            output[4] += "XXX.";
            break;

        case 7:
            output[0] += "XXX.";
            output[1] += "..X.";
            output[2] += "..X.";
            output[3] += "..X.";
            output[4] += "..X.";
            break;

        case 8:
            output[0] += "XXX.";
            output[1] += "X.X.";
            output[2] += "XXX.";
            output[3] += "X.X.";
            output[4] += "XXX.";
            break;

        case 9:
            output[0] += "XXX.";
            output[1] += "X.X.";
            output[2] += "XXX.";
            output[3] += "..X.";
            output[4] += "XXX.";
            break;

        case 0:
            output[0] += "XXX.";
            output[1] += "X.X.";
            output[2] += "X.X.";
            output[3] += "X.X.";
            output[4] += "XXX.";
            break;

        default:
            break;
        }
    }
}
int main()
{
    int n;
    cin >> n;
    string in;
    cin >> in;
    for (int i = 0; i < in.length(); i++)
    {
        int id = in[i];
        add(id - '0');
    }
    // output[0][output[0].length()-1]="";
    output[0][output[0].length() - 1] = '\0';
    output[1][output[1].length() - 1] = '\0';
    output[2][output[2].length() - 1] = '\0';
    output[3][output[3].length() - 1] = '\0';
    output[4][output[4].length() - 1] = '\0';
    cout << output[0] << endl
         << output[1] << endl
         << output[2] << endl
         << output[3] << endl
         << output[4] << endl;
}

标签:...,P5730,..,XXX,break,length,output
From: https://www.cnblogs.com/sweepy/p/16789675.html

相关文章