I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
No time to do this myself...
btw. why not show us your codes?
/*---------------------------------------------------------------------------
File name: TJU1031.cpp
Author: HJin (email: fish_sea_bird [at] yahoo [dot] com )
Created on: 7/10/2007 21:53:53
Environment: Windows XP Professional SP2 English +
Visual Studio 2005 v8.0.50727.762
Modification history:
===========================================================================
7/11/2007 12:20:44
-----------------------------------------------------------
corrected a bug for drawing the digit 3. leeco pointed out this bug. Thanks
go to him.
Problem statement: (http://acm.tju.edu.cn/toj/showp1031.html)
---------------------------------------------------------------------------
1031. LC-Display
--------------------------------------------------------------------------------
Time Limit: 1.0 Seconds Memory Limit: 65536K
Total Runs: 649 Accepted Runs: 210
--------------------------------------------------------------------------------
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 (also the content of my input file TJU1031_Input.txt)
-------------------------------------------------------------------
2 12345
3 67890
4 1234567890
0 0
Sample Output
-- -- --
| | | | | |
| | | | | |
-- -- -- --
| | | | |
| | | | |
-- -- --
--- --- --- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- --- ---
Analysis:
---------------------------------------------------------------------------
(no analysis is needed.)
Sample output:
---------------------------------------------------------------------------
-- -- --
| | | | | |
| | | | | |
-- -- -- --
| | | | | |
| | | | | |
-- -- --
--- --- --- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- ---
| | | | | | | |
| | | | | | | |
| | | | | | | |
--- --- --- ---
---- ---- ---- ---- ---- ---- ---- ----
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
---- ---- ---- ---- ---- ---- ----
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
| | | | | | | | | | | | | |
---- ---- ---- ---- ---- ---- ----
Press any key to continue . . .
Reference:
---------------------------------------------------------------------------
1. http://acm.tju.edu.cn/toj/showp1031.html
*/
#include <iostream>
#include <fstream>
using namespace std;
class Lcd
{
friend std::ostream& operator<<(std::ostream& os, const Lcd& obj);
public:
Lcd(int s_, int n_) : s(s_), n(n_)
{
int i;
int j;
int numRows = 2*s+3;
int numCols = getNumberOfDigits(n)*(s+3);
int q = n;
int r = q % 10;
int counter = 0;
// allocate memory
a = new char*[numRows];
for(i=0; i<numRows; ++i)
{
a[i] = new char[numCols];
}
// initialize the 2d buffer
for(i=0; i<numRows; ++i)
{
for(j=0; j<numCols-1; ++j)
{
a[i][j] = ' ';
}
a[i][j] = '\n';
}
// draw the digits one by one, from last digit to first digit
// (from right to left).
while(q>0)
{
lastCol = numCols - counter*(s+3) - 2;
drawDigit(r);
++counter;
q /= 10;
r = q % 10;
}
}
~Lcd()
{
// deallocate memory
for(int i=0; i<2*s+3; ++i)
{
delete [] a[i];
}
delete [] a;
}
private:
/**
calculate number of digits of a number.
*/
int getNumberOfDigits(int n) const
{
int c = 0; // counter for # of digits
do
{
++c;
n /= 10;
}
while(n>0);
return c;
}
/** Draw a stroke.
@param m index of strokes.
m = 0 --- top horizontal stroke
m = 1 --- left top vertical stroke
m = 2 --- right top vertical stroke
m = 3 --- middle horizontal stroke
m = 4 --- left bottom vertical stroke
m = 5 --- right bottom vertical stroke
m = 6 --- bottom horizontal stroke
*/
void drawStroke(int m)
{
int i;
int j;
int row; // row position
int col; // column position
switch(m)
{
case 0:
case 3:
case 6:
row = m/3*(s+1);
for(j=1; j<=s; ++j)
{
a[row][lastCol-j] = '-';
}
break;
case 1:
case 2:
case 4:
case 5:
row = (m<3 ? 0 : 1)*(s+1)+1;
col = (m % 3 == 1 ? lastCol - s - 1 : lastCol);
for(i=row; i<=row+s-1; ++i)
{
a[i][col] = '|';
}
break;
}
}
/**
Draw a digit r: r =0..9.
*/
void drawDigit(int r)
{
switch(r)
{
case 0:
case 8:
drawStroke(0);
drawStroke(1);
drawStroke(2);
drawStroke(4);
drawStroke(5);
drawStroke(6);
if(r==8)
{
drawStroke(3);
}
break;
case 1:
case 7:
drawStroke(2);
drawStroke(5);
if(r==7)
{
drawStroke(0);
}
break;
case 2:
case 3:
drawStroke(0);
drawStroke(2);
drawStroke(3);
//drawStroke(4); // Bug
drawStroke(6);
if(r==2)
{
drawStroke(4);
}
if(r==3)
{
drawStroke(5);
}
break;
case 4:
case 9:
drawStroke(1);
drawStroke(2);
drawStroke(3);
drawStroke(5);
if(r==9)
{
drawStroke(0);
drawStroke(6);
}
break;
case 5:
case 6:
drawStroke(0);
drawStroke(1);
drawStroke(3);
drawStroke(5);
drawStroke(6);
if(r==6)
{
drawStroke(4);
}
break;
}
}
private:
// disable copy ctor and assignment operator
Lcd(const Lcd&);
Lcd& operator= (const Lcd&);
private:
int s;
int n;
char **a; // 2d buffer
/**
last column for a digit.
*/
int lastCol;
};
std::ostream& operator<<(std::ostream& os, const Lcd& obj)
{
int numRows = 2*obj.s+3;
int numCols = obj.getNumberOfDigits(obj.n)*(obj.s+3);
for(int i=0; i< numRows; ++i)
{
for(int j=0; j< numCols; ++j)
{
os<<obj.a[i][j];
}
}
return os;
}
int main()
{
int s;
int n;
ifstream ifs("TJU1031_Input.txt");
ofstream ofs("TJU1031_Output.txt");
if(!ifs)
{
cout<<"cannot open input file.\n";
exit(0);
}
if(!ofs)
{
cout<<"cannot open output file.\n";
exit(0);
}
while(1)
{
ifs>>s;
ifs>>n;
if( (s==0 && n==0) || s<1 || s> 10 ) // || n<0 || n>99999999 )
break;
Lcd* obj = new Lcd(s, n);
ofs<<*obj<<endl;
cout<<*obj<<endl;
delete obj;
}
ifs.close();
ofs.close();
return 0;
}
[此贴子已经被作者于2007-7-12 3:26:45编辑过]