码迷,mamicode.com
首页 > 其他好文 > 详细

Epic - Decimal Number

时间:2015-06-13 09:45:17      阅读:118      评论:0      收藏:0      [点我收藏+]

标签:

Let the user enter a decimal number. Therange allowed is 0.0001 to 0.9999. Only four decimal places are allowed. Theoutput should be an irreducible fraction. E.g.: If the user enters 0.35,the irreducible fraction will be 7/20.

等于找与10000的公约数

def fraction(d)
  x = d*10000
  gcd = findgcd(x,10000)
  (x/gcd).to_i.to_s + / + (10000/gcd).to_i.to_s
end

def findgcd(a,b)
  return a if b == 0
  findgcd(b,a%b)
end

 

Epic - Decimal Number

标签:

原文地址:http://www.cnblogs.com/lilixu/p/4572954.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!