jmouse 2008-6-13 02:24
2008-06-13 Ruby 测试题(00019)
液晶数字
输入阿拉伯数字串(可以认为小于10位)
输出其液晶表达式。
比如,输入678
输出:
_ _ _
|_ | |_|
|_| | |_|
xavier 2008-6-14 20:04
这题比较有意思~
**** Hidden Message *****
司马睿风 2008-6-15 19:50
参考了下楼上的,但是我的思路也是查表法[code]class LCD
def initialize(scale=1)
@number_map={"0"=>[1,1,1,0,1,1,1],
"1"=>[0,0,1,0,0,1,0],
"2"=>[1,0,1,1,1,0,1],
"3"=>[1,0,1,1,0,1,1],
"4"=>[0,1,1,1,0,1,0],
"5"=>[1,1,0,1,0,1,1],
"6"=>[1,1,0,1,1,1,1],
"7"=>[1,0,1,0,0,1,0],
"8"=>[1,1,1,1,1,1,1],
"9"=>[1,1,1,1,0,1,1]}
@width=scale+2
@height=2*scale+3
light=[[2..2+scale-1,1..1],
[1..1,2..2+scale-1],
[scale+2..scale+2,2..2+scale-1],
[2..2+scale-1,scale+2..scale+2],
[1..1,scale+3..scale*2+2],
[scale+2..scale+2,scale+3..scale*2+2],
[2..2+scale-1,scale*2+3..scale*2+3]]
@light_map={}
7.times do |i|
light[i][0].each do |x|
light[i][1].each do |y|
@light_map["#{x},#{y}"]=i
end
end
end
end [/code]这里是查表,与楼上的不一样感觉这样好看点[code]def display(digits)
digits=digits.to_s
str=""
@height.times do |y|
(digits.length*@width).times do |x|
if x%@width==0 or x%@width==@width-1
c="|"
else
c="-"
end
if @light_map["#{x%@width+1},#{y+1}"]
if @number_map[digits[x/@width..x/@width]][@light_map["#{x%@width+1},#{y+1}"]]==1
str<<c
else
str<<" "
end
else
str<<" "
end
end
str<<"\n"
end
return str
end
end
lcd = LCD.new(2)
puts lcd.display("0024") [/code]
jmouse 2008-6-16 10:55
我想起来了,XAVIER还是被我挑出个小错来。。。
你用的是number,所以不能0开头。
xavier 2008-6-16 18:11
[quote]原帖由 [i]jmouse[/i] 于 2008-6-16 10:55 发表 [url=http://www.ruby-lang.org.cn/forums/redirect.php?goto=findpost&pid=16944&ptid=5153][img]http://www.ruby-lang.org.cn/forums/images/common/back.gif[/img][/url]
我想起来了,XAVIER还是被我挑出个小错来。。。
你用的是number,所以不能0开头。 [/quote]
看来是这样的,0开头的话就被当成八进制了,这个问题从来没注意过,谢谢jmouse了~
也没想到太好的办法,只好规定用字符串了....
**** Hidden Message *****
hexawing 2008-6-20 09:29
好似傻了点……
**** Hidden Message *****
也只能处理“数字”的说……
sandybuster 2008-6-20 13:20
第一次来 还不知道规矩
liumuqing 2008-6-29 19:29
看答案。。。
neohsiao 2008-7-13 12:17
**** Hidden Message *****
[[i] 本帖最后由 neohsiao 于 2008-7-13 12:18 编辑 [/i]]
aztack 2008-9-2 17:00
[code]class String
def to_char_a
r = []
self.each_byte{|e|r<<e .chr}
r
end
end
module LCD
LCD_PATTERN = [" _ ","|_|","|_|"]
LCD_MATRIX =
[
#[0,1,2,3,4,5,6,7,8,9]
[2,0,2,2,0,2,2,2,2,2],#line0
[5,4,6,6,7,3,3,5,7,7],#line1
[7,4,3,6,4,6,7,4,7,6],#line2
]
def to_lcd
final = ""
0.upto(2) do |line|
l = ""
self.to_s.to_char_a.each do |num|
0.upto(2) do|i|
x = LCD_PATTERN[line][i]
y = LCD_MATRIX[line][num.to_i].to_s.unpack("b3").to_s[i].chr.to_i
l += (y==1 ? x.chr : " ")
end
end
final += l+"\n"
end
final
end
end
class Bignum
include LCD
end
class Fixnum
include LCD
end
<e .chr}
puts 12345678901231231.to_lcd[/code]E:\Desktop>lcd.rb
_ _ _ _ _ _ _ _
| _| _||_||_ |_ | ||_||_|| | |
||_ _| | _||_| ||_| _||_| |
[[i] 本帖最后由 aztack 于 2008-9-2 19:57 编辑 [/i]]
Larruping 2008-10-17 23:46
学习,下载下来看看