上のカードと下のカードでn
番号1
が付けられた順序付けられたカードのデッキが与えられます。n
1
n
デッキに少なくとも 2 枚のカードがある限り、次の操作が実行されます。
- 一番上のカードを捨て、今山札の上にあるカードを山札の一番下に移動します。
k
私の仕事は、最後に捨てられたカードと最後に残ったカードの順序を見つけることです。
入力の各行に 2 つの負でない数値が含まれている
n
、 どこn ≤ 5000
k
、 どこk < n
入力行ごとに 2 行の出力を生成します。
k 個の破棄されたカードのシーケンス
最後に残ったカード。
予想される形式については、サンプルを参照してください。
サンプル入力
7 2
19 4
10 5
6 3
4000 7
サンプル入力の出力
Last 2 cards discarded: [4, 2]
Remaining card: 6
Last 4 cards discarded: [2, 10, 18, 14]
Remaining card: 6
Last 5 cards discarded: [9, 2, 6, 10, 8]
Remaining card: 4
Last 3 cards discarded: [5, 2, 6]
Remaining card: 4
Last 7 cards discarded: [320, 1344, 2368, 3392, 832, 2880, 1856]
Remaining card: 3904
私のコードは正確な答えを出力し続けますが、次の行には None があります。
各出力の後に None を出力する理由がとても混乱しています。
これが私のコードです:
def throw_card(n,k):
lst=[]
bst=[]
for i in range(1,n+1):
lst.append(i)
while lst[0]!=lst[1] and len(lst)>1 and n<=5000 and k<n:
bst.append(lst.pop(0))
if len(lst)==1:
break
else:
lst.append(lst[0])
lst.remove(lst[0])
print('Last',k,'cards discarded: ',bst[n-(k+1):])
print('Remaining card: ',lst.pop())
print(throw_card(7,2))
print(throw_card(19,4))
print(throw_card(10,5))
print(throw_card(6,3))
print(throw_card(4000,7))
私の出力:
Last 2 cards discarded: [4, 2]
Remaining card: 6
None
Last 4 cards discarded: [2, 10, 18, 14]
Remaining card: 6
None
Last 5 cards discarded: [9, 2, 6, 10, 8]
Remaining card: 4
None
Last 3 cards discarded: [5, 2, 6]
Remaining card: 4
None
Last 7 cards discarded: [320, 1344, 2368, 3392, 832, 2880, 1856]
Remaining card: 3904
None