LC-Display

110104 LCD Display
A friend of yours has just bought a new computer. Before this, the most powerful machine he
ever used was a pocket calculator. He is a little disappointed because he liked the LCD display of
his calculator more than the screen on his new computer! To make him happy, write a program that
prints numbers in LCD display style.
Input
The input le contains several lines, one for each number to be displayed. Each line contains
integers s and n, where n is the number to be displayed (0  n  99;999;999) and s is the size in
which it shall be displayed (1  s  10). The input will be terminated by a line containing two zeros,
which should not be processed.
Output
Print the numbers speci ed in the input le in an LCD 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 ll all the white space occupied by the digits with blanks, including the
last digit. There must be exactly one column of blanks between two digits.
Output a blank line after each number. You will nd an example of each digit in the sample output
below.
Sample Input
2 12345
3 67890
0 0
Sample Output
-- -- --
| | | | | |
| | | | | |
-- -- -- --
| | | | |
| | | | |
-- -- --
--- --- --- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |

--- --- --- ---


思路:创建一个0-9的size 为1的一个数组,然后根据size的大少进行扩大,若用C来写,需要用一个二维的指针数组。


代码:

#include <stdio.h>

#define MAXLENGTH 8

void display(long size,long number){
   int digits[MAXLENGTH],i,j;
      for(i = 0;i < MAXLENGTH;i++)
         digits[i] = -1;
   if(number == 0)
      digits[MAXLENGTH-1] = 0;
   else
    {
      for(i = MAXLENGTH-1;number >0;i--){
         digits[i] = number % 10;
          number /= 10;       
      }                      
    } 
    
   char *outline[5][10]={
            " - ", "   ", " - ", " - ", "   ", " - ", " - ", " - ", " - ", " - ",  
	        "| |", "  |", "  |", "  |", "| |", "|  ", "|  ", "  |", "| |", "| |",  
	        "   ", "   ", " - ", " - ", " - ", " - ", " - ", "   ", " - ", " - ",  
	        "| |", "  |", "|  ", "  |", "  |", "  |", "| |", "  |", "| |", "  |",  
	        " - ", "   ", " - ", " - ", "   ", " - ", " - ", "   ", " - ", " - "  
        };         
   int row;
   char *n;
   for(row = 1;row <= (2*size+3);row++){
      for(i = 0;i < MAXLENGTH;i++)        
           if(digits[i] != -1){
                      
             if(row ==1 )               
                 n = outline[0][digits[i]];
             if(2 <= row && row < (size + 2))
                 n = outline[1][digits[i]];
             if(row == (size + 2))
                 n = outline[2][digits[i]];
             if ((size + 3) <= row && row <= (2 * size + 2))  
                 n = outline[3][digits[i]];  //对列处理   
             if (row == (2 * size + 3))  
                 n = outline[4][digits[i]];  //对行处理 
             printf("%c",*n);    
             for (j = 0; j < size; j++)
                printf("%c",*(n+1));
             printf("%c",*(n+2));   
             printf(" ");               
           }
           printf("\n");
   }
}

int main(){
   long size,number;
   while(scanf("%d %d",&size,&number) && (size != 0 && number >= 0))
        display(size,number);
   return 0;         
}

个人测试无误,但是提交的时候出现了WA,暂时还没想到哪里出现错误了,不过系统说这道题是没有确切答案的,可能是对的。

你可能感兴趣的:(LC-Display)