用C++输入包括若干行,每行表示一个要显示的数。每行有两个整数s和n (1 <= s <= 10, 0 <= n <= 99999999),这里n是要显示的数,s是要显示的数的尺寸。如果某行输入包括两个0,表示输入结束。这行不需要处理,输出显示的方式是:用s个'-'表示一个水平线段,用s个'|'表示一个垂直线段。这种情况下,每一个数字需要占用s+2列和2s+3行。另外,在两个数字之间要输出一个空白的列。在输出完每一个数之后,输出一个空白的行。注意:输出中空白的地方都要用空格来填充
时间: 2024-03-10 15:43:46 浏览: 45
以下是C++代码实现:
```c++
#include <iostream>
#include <cstring>
using namespace std;
// 定义数字0-9的显示方式
const char digits[10][10][5] = {
{
" - ",
"| |",
" ",
"| |",
" - "
},
{
" ",
" |",
" ",
" |",
" "
},
{
" - ",
" |",
" - ",
"| ",
" - "
},
{
" - ",
" |",
" - ",
" |",
" - "
},
{
" ",
"| |",
" - ",
" |",
" "
},
{
" - ",
"| ",
" - ",
" |",
" - "
},
{
" - ",
"| ",
" - ",
"| |",
" - "
},
{
" - ",
" |",
" ",
" |",
" "
},
{
" - ",
"| |",
" - ",
"| |",
" - "
},
{
" - ",
"| |",
" - ",
" |",
" - "
}
};
int main() {
int s, n;
while (cin >> s >> n && s != 0 && n != 0) {
// 转换数字为字符形式
char num[10];
sprintf(num, "%d", n);
int len = strlen(num);
// 输出每个数字的每一行
for (int i = 0; i < 5; i++) {
for (int j = 0; j < len; j++) {
int d = num[j] - '0';
cout << ' ';
for (int k = 0; k < s; k++) {
if (i == 0) {
cout << digits[d][i][1];
for (int l = 0; l < s; l++) {
cout << digits[d][i][2];
}
cout << digits[d][i][3];
} else if (i == 2) {
cout << digits[d][i][0];
for (int l = 0; l < s; l++) {
cout << ' ';
}
cout << digits[d][i][4];
} else {
cout << digits[d][i][0];
for (int l = 0; l < s; l++) {
cout << ' ';
}
cout << digits[d][i][2];
}
cout << ' ';
}
cout << ' ';
if (j < len - 1) {
cout << ' ';
}
}
cout << endl;
}
// 输出空白行
for (int i = 0; i < s; i++) {
for (int j = 0; j < len * (s + 3) - 1; j++) {
cout << ' ';
}
cout << endl;
}
}
return 0;
}
```
这个程序首先读入每个数字的尺寸s和要显示的数n,然后将n转换成字符形式,并计算出n的长度。接下来,对于每一行数字的显示,我们遍历数字的每一行,并输出每个数字的每一行。输出时,我们需要用空格填充数字和数字之间的空白列,以及数字和边框之间的空白列。最后,输出每个数字后的空白行。注意,本题中每个数字占用的列数为s+2,而不是上面的5。
相关推荐
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)