LoginSignup
0
0

【Python】Atcoder勉強記録⑤

Last updated at Posted at 2023-03-25

atcoder復習

下記にatcoderで解いたB問題を記載します。
勉強の記録として残します。是非参考にしてください。
解いた問題一覧

1.問題①

B - Tournament ResultDifficulty74

解答

N = int(input())
a_list = []
for _ in range(N):
    a = input()
    a_list.append(a)

flg = True

for i in range(N):
    for j in range(N):
        if a_list[i][j] == '-':
            continue
        else:
            if a_list[i][j] == 'W':
                if a_list[j][i] != 'L': # L以外
                    flg = False
            if a_list[i][j] == 'L':
                if a_list[j][i] != 'W':
                    flg = False
            if a_list[i][j] == 'D':
                if a_list[j][i] != 'D':
                    flg = False

if flg:
    print('correct')
else:
    print('incorrect')

解説

入力された文字をlistにセットして二重ループで文字の内容を確認する。
二次元配列のデータにアクセスして次の文字を確認して正しい文字かどうかを判断する。
a_list[i][j] == a_list[j][i]で文字を判断する。iは外側のループで固定でjは内側のループで文字をそれぞれ確認する。
文字が-の場合はjのループを次の行にする。
最後にif文でflgのデータを見て表示する文字を決める。

2.問題②

B - First Query ProblemDifficulty35

解答

N = int(input())
num_list = list(map(int, input().split()))
Q = int(input())
ans_list = []
for i in range(Q):
    q_list = list(map(int, input().split()))
    if len(q_list) == 3:
        n = q_list[1] # 置き換えの位置
        m = q_list[2] # listの最後の要素
        num_list[n - 1] = m
        n_1 = q_list[1]
    else:
        n_1 = q_list[1]
        ans = num_list[n_1 - 1]
        ans_list.append(ans)
for j in ans_list:
    print(j)

解説

num_listを置き換かえながらデータを表示していく
q_listがの要素数が3だったnum_listを置き換えるようにする。
q_listの最後の要素を置き換える。置き換えの位置はq_listの二番目の要素を取得してその場所にあるデータを置き換える。
q_listが3以外の場合はnum_listの要素にアクセスして値を取得する。
ans_listにセットして最終的にfor文で格納したデータを表示する。

3.問題③

C - Cash RegisterDifficulty88

解答

S = input()
ans = len(S)
cnt = 0
for i in S:
    if i == '0':
        cnt += 1
    else:
        cnt = 0
    if cnt == 2:
        cnt = 0
        ans -= 1
print(ans)

解説

最初ansに入力した文字列の文字数を格納する。格納した文字をfor文で確認して文字が0だった場合は確認フラグにプラス1をする。
それ以外の場合は確認フラグを0に戻す。
0が二つ(確認フラグが2)になった場合はansの文字数を1引いていく。
最終的に集計したansを表示する。

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