最小数位和
要使得数位和达到要求并且最小,那就是刚好等于x值,即n值从后往前填充,逢9向前移一进位。
def min_bit_sum(x):
if x < 10:
return x
res = 0
base = 1
while x:
if x >= 9:
res += (9 * base)
x -= 9
else:
res += (x * base)
x = 0
base *= 10
return res
if __name__ == '__main__':
t = int(input().strip())
for _ in range(t):
x = int(input().strip())
res = min_bit_sum(x)
print(res)
'''
2
7
9
2
13
18
'''
吃葡萄
懒人模式,暴力求解。超时提醒,通过率为60%,后面尝试用total//3
来约束遍历次数,依旧是这样的结果。
def eat_number_of_three(seq):
if not seq:
return 0
a, b, c = seq
total = sum(seq)
res = total
for i in range(0, a+b+1):
if i <= a:
cj = b
else:
cj = a + b - i
for j in range(0, cj + c + 1):
k = total - i - j
# print(i, j, k)
cur = max([i, j, k])
if cur < res:
res = cur
return res
if __name__ == '__main__':
t = int(input().strip())
for _ in range(t):
seq = list(map(int, input().strip().split()))
res = eat_number_of_three(seq)
print(res)
'''
2
1 2 3
1 2 6
1
12 13 11
'''
完美的序列
节省时间的关键步骤是当前值小于和值但大于前一个值的时候,进行了一次向前遍历至合乎条件。
def perfect_sequence(seq):
if len(seq) < 2:
return len(seq)
value, current, index = seq[0], seq[0], 0
res = []
for si in range(1, len(seq)):
su = seq[si]
if su >= current:
current += su
value = su
else:
if su < value:
res.append(si-index)
index = si
value, current = su, su
else:
cur = su
index = si - 1
while index >= 0 and cur > seq[index]:
cur -= seq[index]
index -= 1
index += 1
value, current = seq[index+1], sum(seq[index+1:si+1])
if index <= si:
res.append(si-index+1)
# print(res)
return max(res)
if __name__ == '__main__':
t = int(input().strip())
for _ in range(t):
n = int(input().strip())
seq = list(map(int, input().strip().split()))
res = perfect_sequence(seq)
print(res)
'''
2
5
1 3 9 2 6
5
4 2 9 16 7
'''
圆环切割
懒人模式,暴力求解,超时提醒,通过率为60%,后面尝试在刚好不满足条件处进行比较后一个与前一个值的情况来进行优化,但后面还有两个问答题,时间不多,就提交了,结果两个问答题也未免是出来搞笑的吧,嗯,提交半个小时交卷了!
def circle_split_into_area(seq, n):
if sum(seq) % 2 == 1:
return 'NO'
target = sum(seq) // 2
for si in range(n):
current = seq[si:] + seq[:si]
value, index = current[0], 1
while index < n and value+current[index] <= target:
value += current[index]
index += 1
if value == target:
return 'YES'
# print(si, current, index)
return 'NO'
if __name__ == '__main__':
t = int(input().strip())
for _ in range(t):
n = int(input().strip())
seq = list(map(int, input().strip().split()))
res = circle_split_into_area(seq, n)
print(res)
'''
1
6
1 2 3 4 5 6
1
4
4 4 5 3
'''
(最近更新:2019年09月21日)