0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

paizaラーニング問題集「【全探索 1】高い寿司を食いたい!」を、解説例に基づいて解いてみた

Posted at

▼解説例の考え方

連続するK枚の寿司を、P[(i+j)%N] (j=0,1,...K-1)で表現する考え方です。以下に例を示します。

image.png

▼コード

########## 処理0(準備) インプット,リスト定義 ###########

N,K = map(int,input().split())

P = [0]*N
for i in range(N):
    P[i] = int(input())

# tmp_total: 連続するK枚の寿司の合計金額を格納する変数
tmp_total = 0

max_prime = 0

########## 処理1 連続するK枚の寿司の合計金額および最大値の計算、出力 ###########

for i in range(N):

    # j: 連続するK枚の寿司を走査する変数
    for j in range(K):
        tmp_total += P[(i+j)%N]
    
    if tmp_total > max_prime:
        max_prime = tmp_total

    tmp_total = 0

print(max_prime)

▼参考

以下は、本問を解説例ではなく、私の考え方に基づいて解いてみた記事です。
https://qiita.com/toytoy24/items/5792df63ae7195e09c4e

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?