1. 立方和
hashmap = {i**3 for i in range(1, 1000)}
for _ in range(int(input())):
number = int(input())
for i in range(1, number):
remain = number - i ** 3
if remain < 0:
print('No')
break
elif remain in hashmap:
print('Yes')
break
2. 矿泉水瓶,示例通过了,但提交时通过0%....
有谁知道为啥吗
import math
for _ in range(int(input())):
x, a, b = map(int, input().split())
n = math.ceil(x / 500)
if b >= 3 * a:
money = n * a
else:
money = (n // 3) * b + (n % 3) * a
print(money)
错误原因:最后剩下的部分应该是
min((n % 3) * a, b)
而非
(n % 3) * a
修正后的代码:
import math
for _ in range(int(input())):
x, a, b = map(int, input().split())
n = math.ceil(x / 500)
if b >= 3 * a:
money = n * a
else:
money = (n // 3) * b + min((n % 3) * a, b)
print(money)
3. 求环的个数
for _ in range(int(input())):
N = int(input())
a = [int(x) for x in input().split()]
amap = dict(enumerate(a, start=1))
loop_count = 0
while amap:
tmp = {}
k, v = amap.popitem()
while v not in tmp:
tmp[k] = v
if v in tmp:
break
else:
k, v = v, amap.pop(v)
loop_count += 1
if loop_count == 1:
print(0)
elif loop_count > 1:
print(loop_count)
#深信服笔试题#