问题
燃油喷射完美 拉姆达指挥官已经请求你帮助完善自动量子反物质燃油喷射系统,为她的兰姆斩末日装置。这对你来说是一个很好的机会,可以让你更近距离地看一看羊排--也许你在这的时候会偷偷地搞点破坏--所以你很高兴地接受了这份工作。 量子反物质燃料以小颗粒的形式出现,这是很方便的,因为兰姆切普的许多移动部分需要一次给燃料一个球。然而,奴才将散装的球团倾倒到燃料的进气口。你需要找出最有效的方法来分类,并在一次将颗粒转移到单个颗粒。 燃料控制机制有三项运作: 添加一个燃料颗粒,除去一个燃料颗粒,将整个燃料颗粒组除以2(由于量子反物质颗粒被切成两半时释放出的破坏性能量,安全控制只允许这种情况发生,如果有偶数的颗粒),编写一个名为答案(N)的函数,该函数以正整数为字符串,并返回将颗粒数量转换为1所需的最小操作数。燃料摄入控制面板只能显示309位长的数字,因此不会有比这几位数更多的颗粒。 例如:应答(4)返回2: 4 -> 2 -> 1答案( 15 )返回5: 15 -> 16 -> 8 -> 4 -> 2 -> 1 测试用例 输入:(字符串)n= "4“输出:(int) 2 输入:(字符串)n= "15“输出:(int) 5
这是我的解决方案:
import math
import decimal
def answer(n):
n = long(n)
if n <= 1 or n >= long('9' * 309):
return 0
# 1. Find closest power of 2
round_threshold_adjust = math.log(3,2)- (1.5)
log_n = math.log(n, 2)
power = log_n - round_threshold_adjust
# Round power down if X.50000. If n is equally between two powers of 2,
# choose the lower power of 2. E.g. For 6, choose, 4 not 8
power2 = long(decimal.Decimal(power).quantize(0, decimal.ROUND_HALF_DOWN))
# 2. Calculate the difference, add to iterations
# 3. Take log 2 of that, add that to iteration
iters = abs(n - 2**power) + power
return(iters)我的解决方案目前通过了10个测试用例中的3个。我相信其他测试用例都是边缘用例。请您给我一些提示,说明我如何识别我的代码在哪里出错?(我无法访问测试用例)
下面是我尝试过的一些测试用例:
assert answer(15) == 5
assert answer(4) == 2
assert answer(3) == 2
assert answer(2) == 1
assert answer(6) == 4
assert answer(7) == 4
assert answer(10) == 5
assert answer(1024) == 10
assert answer(1025) == 11
assert answer(1026) == 12
assert answer(1027) == 13
assert answer(768) == 256 + 9发布于 2017-08-21 08:16:34
如果我的理解是正确的,给出768个球团,你需要256+9步骤才能把它转换成1个球团?
我可以分十步完成:
我认为你的第一步,加/减,直到你以2的力量降落,不是最快的解决办法。
我不知道如何编写更好的解决方案,但这可能会为您指明正确的方向。直观地说,我的下一步将是查看数字的二进制表示,并将允许的操作转换为该表示。这可能简化正确算法的创建。
发布于 2019-08-15 04:41:57
这是我的解决方案:
#!/usr/bin/python
#fuel-injection-perfection
#Program to count the naximum number of operations needed to recursively divide a number by 2. Add or subtract 1 where needed.
#V2 - Initial version was done using recursion but failed for large numbers due to python limitation & performance issues.
cnt=0
def divide(x):
global cnt
while(x>1):
if (x & 1==0):
#Dividing even number by two by shifting binary digits one step to the right.
x=x>>1
else:
a=x+1
b=x-1
#counters ac & bc will be used to count trailing 0s
ac=bc=0
#count trailing 0's for x+1
while(a & 1==0):
a=a>>1
ac+=1
#count trailing 0's for x-1
while(b & 1==0):
b=b>>1
bc+=1
#go with x+1 if it has more trailing 0s in binary format. Exception is number 3 as b10 can be divided in less steps than b100.
#edge case 3 identified by manually testing numbers 1-10.
if (ac>bc and x!=3):
x+=1
else:
x-=1
cnt+=1
def solution(n):
global cnt
n=int(n)
divide(n)
return cnt发布于 2020-06-12 18:27:05
这是Python3的答案。我创建了两个函数,一个加1,另一个减1,然后比较,用最少的步骤给出最好的答案。
#function for the adding 1 to the odd number
def np(n):
c = 0
while n > 1:
if n%2 == 0:
n = n/2
c += 1
else:
n += 1
c += 1
return c
#function for the subtracting 1 to the odd number
def nn(n):
c = 0
while n > 1:
if n%2 == 0:
n = n/2
c += 1
else:
n -= 1
c += 1
return c
#Solution function
def solution(n):
n = int(n)
if np(n) > nn(n):
return nn(n)
else:
return np(n)希望这能成功,干杯。
https://stackoverflow.com/questions/45790229
复制相似问题