| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1255 人关注过本帖
标题:挑战TJU1031
只看楼主 加入收藏
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(stupid_boy)以下是引用leeco在2007-7-11 16:...
very good. will do it later myself.


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-11 18:49
野比
Rank: 7Rank: 7Rank: 7
等 级:贵宾
威 望:24
帖 子:1627
专家分:516
注 册:2007-5-24
收藏
得分:0 

No time to do this myself...

btw. why not show us your codes?


女侠,约吗?
2007-07-11 20:02
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
回复:(野比)No time to do this myself...btw. why...
把我的show了想挑战的人还怎么挑战啊,思路都按照我的走了,只要把我的改一下就更短了,事实上我已经改到660字节了,基本没有任何可读性……
2007-07-11 20:47
stupid_boy
Rank: 1
等 级:新手上路
帖 子:53
专家分:0
注 册:2007-7-6
收藏
得分:0 

照你这样发展下去。。。内存市场和硬盘市场都要重新洗牌了。。。

偶觉得写程序现在不必过分的简短导致失去可读性

偶觉得写别人看得懂的程序才是正道

谭浩强也是这样说的嘛

[此贴子已经被作者于2007-7-11 21:32:19编辑过]


失眠。。。
2007-07-11 21:18
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
回复:(stupid_boy)照你这样发展下去。。。内存市场...
程序员的创造力和代码的可维护性成反比。
是我在《高效程序设计的奥秘》里看到类似的话。

在毕业之前我还是坚持注重创造力和程序的高效。
2007-07-11 22:26
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
回复:(leeco)挑战TJU1031

/*---------------------------------------------------------------------------
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编辑过]


I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-11 23:11
leeco
Rank: 4
等 级:贵宾
威 望:10
帖 子:1029
专家分:177
注 册:2007-5-10
收藏
得分:0 
看了一下,您似乎多写了一句drawStroke(4);导致3的输出有问题。
2007-07-12 02:26
HJin
Rank: 6Rank: 6
等 级:贵宾
威 望:27
帖 子:401
专家分:0
注 册:2007-6-9
收藏
得分:0 
thanks, the mistake is corrected now.

I am working on a system which has no Chinese input. Please don\'t blame me for typing English.
2007-07-12 03:27
快速回复:挑战TJU1031
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.023473 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved