ZJU1146解题报告

来源:互联网 发布:js中事件传播的方式 编辑:程序博客网 时间:2024/04/28 21:42
LC-Display

Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 1881   Accepted Submit: 545  

A friend of you has just bought a new computer. Until now, the most powerful computer he ever used has been a pocket calculator. Now, looking at his new computer, he is a bit disappointed, because he liked the LC-display of his calculator so much. So you decide to write a program that displays numbers in an LC-display-like style on his computer.


Input

The input contains several lines, one for each number to be displayed. Each line contains two integers s, n (1 <= s <= 10, 0 <= n <= 99 999 999), where n is the number to be displayed and s is the size in which it shall be displayed.

The input file will be terminated by a line containing two zeros. This line should not be processed.


Output

Output the numbers given in the input file in an LC-display-style using s ``-'' signs for the horizontal segments and s ``|'' signs for the vertical ones. Each digit occupies exactly s+2 columns and 2s+3 rows. (Be sure to fill all the white space occupied by the digits with blanks, also for the last digit.) There has to be exactly one column of blanks between two digits.

Output a blank line after each number. (You will find a sample of each digit in the sample output.)


Sample Input

2 12345
3 67890
0 0


Sample Output

      --   --        --
   |    |    | |  | |
   |    |    | |  | |
      --   --   --   --
   | |       |    |    |
   | |       |    |    |
      --   --        --

 ---   ---   ---   ---   ---
|         | |   | |   | |   |
|         | |   | |   | |   |
|         | |   | |   | |   |
 ---         ---   ---
|   |     | |   |     | |   |
|   |     | |   |     | |   |
|   |     | |   |     | |   |
 ---         ---   ---   ---

 


Problem Source: Mid-Central European Regional Contest 1999 

 

题目地址:http://acm.zju.edu.cn/show_problem.php?pid=1146

本题要求输入数字,用电子表的显示方式输出。我的思路是先把0-9这九个数字的电子表显示形式保存在一个三维数组里,第一维为选择数字0-9,第二维选择电子表显示形式的第几层(0-4),第三维选择输出的字符,0为空格,1为横线,2为竖线;然后定义几个函数:

int dig(int s, int len, int p) //返回当前输出的数字

int row(int s, int i)  //返回当前输出调用数组第二维电子表显示形式的层数

int col(int s, int i, int len)  //返回当前输出是总输出中的第几个数字,-1为不是任一个,即数字中间的空格

int chr(int s, int j, int c)  //返回当前输出是字符的哪一部分(0-2)

最后在main()中用循环控制即可。

参考代码如下:

#include <stdio.h>

static char num[10][5][3= {
    
{010202000202010}
    
{000002000002000}
    
{010002010200010}
    
{010002010002010},
    
{000202010002000},
    
{010200010002010}
    
{010200010202010}
    
{010002000002000}
    
{010202010202010},
    
{010202010002010}
}
;

int dig(int s, int len, int p)
{
    
int t = len - p;
    
while (t --) s /= 10;
    
return s % 10;
}


int row(int s, int i)
{
    
if (!i) return 0;
    
if (i > 0 && i <= s) return 1;
    
if (i == s + 1return 2;
    
if (i > s + 1 && i < 2 * s + 2return 3;
    
if (i == 2 * s + 2return 4;

    
return -1;
}


int col(int s, int i, int len)
{
    
int j;

    
for (j = 0; j < len; j ++
        
if (i >= j * (3 + s) && i <= j * (3 + s) + s + 1return j;

    
return -1;
}


int chr(int s, int j, int c)
{
    
if (j == c * (3 + s)) return 0;
    
if (j == c * (3 + s) + s + 1return 2;
    
return 1;
}


int main(int argc, char *argv[])
{
    
int i, j, k, s, m, n, r, c, t, len;

    
while (scanf("%d %d"&s, &n) && (s || n)) {
        
for (t = n, len = 0; t; len ++) t /= 10;
        
if (n == 0) len = 1;
        
for (i = 0; i < 3 + 2 * s; i ++{
            
for (j = 0; j < (3 + s) * len - 1; j ++{
                r 
= row(s, i);
                c 
= col(s, j, len);
                k 
= dig(n, len, c + 1);
                m 
= chr(s, j, c);
                
if (c == -1) printf(" ");
                
else switch(num[k][r][m]) {
                    
case 0: printf(" ");
                        
break;
                    
case 1: printf("-");
                        
break;
                    
case 2: printf("|");
                        
break;
                    
defaultbreak;
                }

            }

            printf(
"/n");
        }

        printf(
"/n");
    }


    
return 0;
}
原创粉丝点击