LoginSignup
0
2

More than 3 years have passed since last update.

AtCoder Beginner Contest 081

Posted at

問題文
黒板に
N
個の正の整数 A1.......ANが書かれている
すぬけ君は,黒板に書かれている整数がすべて偶数であるとき,次の操作を行うことができます.

黒板に書かれている整数すべてを,
2で割ったものに置き換える.
すぬけ君は最大で何回操作を行うことができるかを求めてください.

答え

# 整数の入力
length = int(input())
numbers = map(int, input().split())

リストを使うと下記のように出力される。

print(list(numbers))

[5, 6, 8, 10]

これをこのままmap関数で一つずつ処理をしてあげたい

引数が配列、何回処理できるか返す関数を作る

#回数をまずは0で定義
count = 0
# 繰り返し処理。偶数なら〜
while n % 2 == 0:
n /= 2
#回数を1上げる
count += 1
#割れなくなったら返す
return count

最大で何回操作を行うことができるかなので、帰ってきた値の中で一番小さい数を出力する

ans = min(map(how_many_times_divisible, a))
#答え
print ans

引数が配列、何回処理できるか返す関数を作る
def how_many_times_dibisible(n):

学んだこと
・繰り返し処理の種類
・returnの使い方
感想
繰り返し文でfor文ではなくwhile文を使うところがいまいちしっくりきていない
最大で何回操作を行ったかの問題で最小の値を返すのは逆説的で面白い

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