问题描述
我正在寻找一种算法来找到两个整数值 x,y
,这样他们的产品尽可能接近给定的双倍 k
虽然差异很小。
I'm looking for an algorithm to find two integer values x,y
such that their product is as close as possible to a given double k
while their difference is low.
示例:矩形区域为 k = 21.5
我希望找到该矩形的边长,其约束条件必须是整数,在这种情况下,一些可能的解决方案是(排除排列)( x = 4,y = 5)
,(x = 3,y = 7)
和愚蠢的解决方案(x = 21,y = 1)
Example: The area of a rectangle is k=21.5
and I want to find the edges length of that rectangle with the constraint that they must be integer, in this case some of the possible solutions are (excluding permutations) (x=4,y=5)
,(x=3,y=7)
and the stupid solution (x=21,y=1)
实际上对于(3,7)
情侣我们与(21,1)
情侣有相同的差异
In fact for the (3,7)
couple we have the same difference as for the (21,1)
couple
21.5-3 * 7 = 0.5 = 21.5-21 * 1
而(4,5)
情侣
21.5-4 * 5 = 1.5
但情侣(4,5)
是首选,因为它们的差异是 1
,因此矩形是更加平方。
but the couple (4,5)
is preferable because their difference is 1
, so the rectangle is "more squared".
有没有方法d提取那些 x,y
的值,这些值的差异是最小的,他们的产品与k的差异也是最小的?
Is there a method to extract those x,y
values for which the difference is minimal and the difference of their product to k is also minimal?
推荐答案
您必须查看相关数字的平方根。对于21.5 sqrt(21.5)= 4.6368,实际上你找到的数字就在这个值附近。
You have to look around square root of the number in question. For 21.5 sqrt(21.5) = 4.6368 and indeed the numbers you found are just around this value.
这篇关于找到两个整数,使他们的产品接近给定的真实的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!