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

· · 题解

提供一个和其他题解不太一样的做法。

先预处理。

string s[] = {"XXX","..X","XXX","XXX","X.X","XXX","XXX","XXX","XXX","XXX"};
string t[] = {"X.X","..X","..X","..X","X.X","X..","X..","..X","X.X","X.X"};
string u[] = {"X.X","..X","XXX","XXX","XXX","XXX","XXX","..X","XXX","XXX"};
string v[] = {"X.X","..X","X..","..X","..X","..X","X.X","..X","X.X","..X"};
string w[] = {"XXX","..X","XXX","XXX","..X","XXX","XXX","..X","XXX","XXX"};

在输出时判断是第几层就行了,然后要判断是否要输出 . 。

较短的代码奉上 :

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

int n;
char a[105];

string s[] = {"XXX","..X","XXX","XXX","X.X","XXX","XXX","XXX","XXX","XXX"};
string t[] = {"X.X","..X","..X","..X","X.X","X..","X..","..X","X.X","X.X"};
string u[] = {"X.X","..X","XXX","XXX","XXX","XXX","XXX","..X","XXX","XXX"};
string v[] = {"X.X","..X","X..","..X","..X","..X","X.X","..X","X.X","..X"};
string w[] = {"XXX","..X","XXX","XXX","..X","XXX","XXX","..X","XXX","XXX"};

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    cin >> n;

    for (int i = 0; i < n; i++) {
        cin >> a[i];
    }

    for (int i = 1; i <= 5; i++) {
        for (int j = 0; j < n; j++) {
            if (i == 1) cout << s[a[j] - '0'];
            if (i == 2) cout << t[a[j] - '0'];
            if (i == 3) cout << u[a[j] - '0'];
            if (i == 4) cout << v[a[j] - '0'];
            if (i == 5) cout << w[a[j] - '0'];
            if (j < n - 1) cout << '.';
        }
        cout << endl;
    }
    return 0;
}