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
时间: 2024-11-09 22:05:32