要使得數字和達到要求並且最小,那就是剛好等於x值,即n值從後往前填充,逢9向前移一進製。
def
min_bit_sum
(x):
if x <10:
return x
res =
0 base =
1while x:
if x >=9:
res +=(9
* base)
x -=
9else
: res +=
(x * base)
x =0 base *=
10return res
if __name__ ==
'__main__'
: t =
int(
input()
.strip())
for _ in
range
(t):
x =int(
input()
.strip())
res = min_bit_sum(x)
print
(res)
'''279
21318'''
懶人模式,暴力求解。超時提醒,通過率為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
112 13 11
'''
節省時間的關鍵步驟是當前值小於和值但大於前乙個值的時候,進行了一次向前遍歷至合乎條件。
def
perfect_sequence
(seq):if
len(seq)
<2:
return
len(seq)
value, current, index = seq[0]
, seq[0]
,0res =
for si in
range(1
,len
(seq)):
su = seq[si]
if su >= current:
current += su
value = su
else
:if su < value:
index = si
value, current = su, su
else
: cur = su
index = si -
1while index >=
0and cur > seq[index]
: cur -= seq[index]
index -=
1 index +=
1 value, current = seq[index+1]
,sum
(seq[index+
1:si+1]
)if index <= si: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
51 3 9 2 6
54 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]
,1while index < n and value+current[index]
<= target:
value += current[index]
index +=
1if 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
61 2 3 4 5 614
4 4 5 3
'''
(最近更新:2023年09月21日) 網易有道筆試題
這幾道題都是從網上找的不知道是哪一年的網易有道的筆試題,拿來做做,練練手。1 列印如下形式的矩陣 n 5 1 2 9 10 25 4 3 8 11 24 5 6 7 12 23 16 15 14 13 22 17 18 19 20 21 n 6 1 2 9 10 25 26 4 3 8 11 24 ...
網易春招筆試題
網易2017年3月26日中午的實習生招聘裡面有道程式設計題,大概是說有乙個圍棋格仔,每乙個塊為白色或黑色,小易會找出列方向上顏色相同並且連著的區域,並最長,將其塗為紅色。第一行輸入棋盤的大小,如3,後面依次是棋盤的顏色,w是white白色,b是black黑色 bwbbbb bww 那麼很明顯第一列有...
網易筆試題 Fibobacci數列
fibonacci數列是這樣定義的 f 0 0 f 1 1 for each i 2 f i f i 1 f i 2 因此,fibonacci數列就形如 0,1,1,2,3,5,8,13,在fibonacci數列中的數我們稱為fibonacci數。給你乙個n,你想讓其變為乙個fibonacci數,每...