问题描述
你的一个朋友买了一台电脑。他以前只用过计算器,因为电脑的显示器上显示的数字的样子和计算器是不一样,所以当他使用电脑的时候会比较郁闷。为了帮助他,你决定写一个程序把在电脑上的数字显示得像计算器上一样。
输入
输入包括若干行,每行表示一个要显示的数。每行有两个整数 s 和 n (1 <= s <= 10, 0 <=n <= 99999999),这里 n 是要显示的数,s 是要显示的数的尺寸。 如果某行输入包括两个 0,表示输入结束。这行不需要处理。
输出
显示的方式是:用 s 个'-'表示一个水平线段,用 s 个'|'表示一个垂直线段。这种情况下,每一个数字需要占用 s+2 列和 2s+3 行。另外,在两个数字之间要输出一个空白的列。在输出完每一个数之后,输出一个空白的行。注意:输出中空白的地方都要用空格来填充。
输入样列
2 12345
3 67890
0 0
输出样例
-- -- --
| | | | | |
| | | | | |
-- -- -- --
| | | | |
| | | | |
-- -- --
--- --- --- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- --- ---
算法实现
using System;
namespace Questions{
class Program{
public static void Main(string[] args){
while (true){
string input = Console.ReadLine();
string[] data = input.Split();
int s = int.Parse(data[0]);
int k = int.Parse(data[1]);
if (s == 0 && k == 0)
break;
int l = data[1].Length;
int[,] num = new int[2 * s + 3,(s + 2) * l+l-1];
for (int i = 0; i < l; i++) {
char temp = data[1][i];
switch (temp) {
case '0':
{
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
{
num[m, (s + 2) * (i + 1) + i - 1] = 2;
num[m, (s + 2) * i + i] = 2;
}
for (int n = (s + 2) * i + 1 + i; n < (s + 2) * (i + 1) - 1 + i; n++)
{
num[0, n] = 1;
num[2 * s + 2, n] = 1;
}
break;
}
case '1':
{
for (int m = 0;m<2*s+3;m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
break;
}
case '2':
{
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
{
num[0,n] = 1;
num[ s + 1,n] = 1;
num[2 * s + 2,n] = 1;
}
for (int m = 1; m <= s ; m++)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
for (int m = s+2; m < 2*s+2; m++)
num[m,(s + 2) * i + i] = 2;
break;
}
case '3':
{
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 &&m !=2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
{
num[0,n] = 1;
num[s + 1,n] = 1;
num[2 * s + 2,n] = 1;
}
break;
}
case '4':
{
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
num[s + 1,n] = 1;
for (int m = 1; m <= s; m++)
num[m,(s + 2) * i + i] = 2;
break;
}
case '5':
{
for (int m = 1; m <= s; m++)
if (m != s + 1)
num[m,(s + 2) * i + i] = 2;
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
{
num[0, n] = 1;
num[s + 1, n] = 1;
num[2 * s + 2, n] = 1;
}
for (int m = s+2; m < 2 * s + 2; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
break;
}
case '6':
{
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * i + i] = 2;
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
{
num[0, n] = 1;
num[s + 1, n] = 1;
num[2 * s + 2, n] = 1;
}
for (int m = s + 2; m < 2 * s + 2; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
break;
}
case '7':
{
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
num[ 0,n] = 1;
for (int m = 0; m < 2 * s + 3; m++)
{
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
}
break;
}
case '8':
{
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
{
num[0, n] = 1;
num[s + 1, n] = 1;
num[2 * s + 2, n] = 1;
}
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * i + i] = 2;
break;
}
case '9':
{
for (int m = 0; m < 2 * s + 3; m++)
if (m != 0 && m != s + 1 && m != 2 * s + 2)
num[m,(s + 2) * (i + 1) - 1 + i] = 2;
for (int n = (s + 2) * i+1 + i; n < (s + 2) * (i + 1)-1+i; n++)
{
num[0, n] = 1;
num[s + 1, n] = 1;
num[2 * s + 2, n] = 1;
}
for (int m = 1; m <= s; m++)
num[m,(s + 2) * i + i] = 2;
break;
}
}
}
for (int n = 0; n < 2 * s + 3; n++)
{
for (int m = 0; m < (s + 2) * l + l - 1; m++)
if(num[n,m]==1)
Console.Write("-");
else if(num[n, m] == 2)
Console.Write("|");
else
Console.Write(" ");
Console.WriteLine();
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
网友评论