然而,可能已知的问题是,由于缺乏知识和糟糕的英语,我无法正确地查询这样的问题。
给定正整数N
,找到非负整数a,b,c
,使N = a*b + c
最小化c
。尊重以下内容:
a <= b
(b / a) < 2
c <= (a / 2)
N = 24 -> a = 4, b = 6, c = 0
N = 25 -> a = 5, b = 5, c = 0
N = 26 -> a = 5, b = 5, c = 1
N = 27 -> a = 5, b = 5, c = 2
N = 28 -> a = 4, b = 7, c = 0
N = 29 -> a = 4, b = 7, c = 1
N = 30 -> a = 5, b = 6, c = 0
N = 31 -> a = 5, b = 6, c = 1
N = 32 -> a = 5, b = 6, c = 2
答案 0 :(得分:1)
你不会说哪种语言,所以我会做伪代码。
function findFactors(num) {
bestFactors = (0,0)
bestRemainder = num
for (x=num; x>0; x--) {
for (y=num; y>0; y--) {
if (x*y <= num) {
if (num % (x*y) < bestRemainder) {
bestFactors =(x,y)
}
}
}
}
return bestFactors
}
不可否认的是,这个方法有一个很大的n平方,这个方法有点不守规矩。这可能是一种更好的递归方式,但我现在并不打算花那么多脑力。
答案 1 :(得分:0)
N
是素数返回对((N-1) / 2, 2)
N
,N = p1 * p2 * ... * pk, pi != 1
返回(p1, p2 * ... * pk)
对于此算法c <= 1