|
发表于 2007-9-17 14:03:30
|
显示全部楼层
我用ruby 写的:
def count_1x10(d)
return 1 if d==0
return 2 if d==1
return 21 if d==2
s = d.to_s
(d-2).times {s<<'0'}
s << '1'
return s.to_i
end
def count_nx10(n,d)
return 0 if n==0
return 1 if d==0
return 2 if d==1 && n==1
return 10+n if d==1
return count_1x10(d) if n==1
m = count_1x10(d)-1
return 10**d + m*n
end
def count_number(n)
raise "n can not less than 0!!! =>" + n.to_s if n<0
return 0 if n==0
return 1 if n<10
s = n.to_s
m = s.length-1
re = 0
s.each_byte do |c|
re += count_nx10(c.chr.to_i,m)
m -= 1
end
pos = s.index('1')
while pos && pos!=(s.length-1)
re += s.slice(pos+1,s.length).to_i
pos = s.index('1',pos+1)
end
return re
end
puts count_number(ARGV[0].to_i) |
|