对于给定的数字n,找出可以用两个给定数字(a,b)和n的倍数形成的下一个最近数字之间的差异。找到n的最近的下一个数字,其中数字的总和为2的倍数
Example:
n = 49, (a, b) = (13, 17) => Difference = 2
Nearest number would be = 51 (3*17, 0*13)
n = 16, (a, b) = (2 , 5) => Difference = 0
Nearest number would be = 16 (2*5, 3*2)
n = 25, (a, b) = (13, 17) => Difference = 1
Nearest number would be = 26 (0*17, 2*13)
我该如何解决这个问题?
我所写的是:(在红宝石)
def find_next_num_diff(x,y,z)
x, y = x > y ? [x, y] : [y, x]
while(z%y > 0 && z >= x) do
z -= x
end
if z%y == 0
return 0
else
return [y-(z%y), x-z].min
end
end
上面的代码不会为过去的那种例子工作。
编辑: 没有负数。只有总和。 起初我以为这个问题是解决了X & Y
的方程 Xa + Yb >= n
和X, Y > 0
如果这是语言不可知的,则应删除所有语言标记。否则,选择一个。 – yano
你允许负倍数吗?即25 = 17 * 3-13 * 2 => n = 25,(a,b)=(13,17)=>差= 0 –
考虑到'n'是一个整数,它总是'n'。或者你错过了这个问题中的一些信息。 – Olaf