LoginSignup
5
2

More than 5 years have passed since last update.

Pythonで競プロに挑む日誌 vol.2 ~all, any 関数. generator との出会い~

Last updated at Posted at 2018-08-30

現在の目標

  • 完了: AtCoder をはじめる
  • 今年の10月内に茶色を取得する ←イマココ
  • 年内に緑色を取得する
  • APG4b で C++ にも手を出す

本日の問題1

ABC081A - Placing Marbles

https://beta.atcoder.jp/contests/abs/tasks/abc081_a
結果

answer.py
s = list((input()))
print(s.count("1"))

回答時間:たぶん15秒くらい
実行時間:17 ms
メモリ :2940 KB
得点  :100/100

特にコメントは不要と思います。

本日の問題2

ABC081B - Shift only

https://beta.atcoder.jp/contests/abs/tasks/abc081_b
結果

answer_1st.py
# coding: utf-8
N = int(input())
lis_A = list(map(int, input().split()))

cnt = 0
while True:
    for i in range(N):
        if (lis_A[i] % 2) != 0:
            break
        else:
            lis_A[i] = lis_A[i] // 2
    # while 文の break 判定
    if i == N - 1:
        cnt += 1
    else:
        break

print(cnt)

回答時間:20分くらい
実行時間:18 ms
メモリ :3060 KB
コード長:342 Byte
得点  :200/200

愚直な実装ですね笑。while 文と for 文の break の入れ方でやや混乱したので、print 関数を使いながらコツコツと…こうやって感覚を養っていくものなのでしょう。
もっとうまいやり方がありそうに思ったのでもう少し粘ってみました↓

answer_2nd.py
# coding: utf-8
N = int(input())
li_A = list(map(int, input().split()))

cnt = 0
while True:
    li_yojo = [ai % 2 for ai in li_A]   # 余剰の 0 の個数で判定
    if li_yojo.count(0) != N:
        break
    li_A = [ai / 2 for ai in li_A]
    cnt += 1

print(cnt)

回答時間:10分
実行時間:18 ms
メモリ :2940 KB
コード長:281 Byte
得点  :200/200

リスト内包表記を使用したらすっきりしました。
参考:https://qiita.com/y__sama/items/a2c458de97c4aa5a98e7
一気に余剰計算して、一気に判定することにしました。速さは変わってませんが、コードを短くできたのでよしとしましょう。内包表記は重宝しそうなので、慣れていきたいですね。

明日やること

ABC087B - Coins を解く。

5
2
7

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
5
2