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?

Atcoder 備忘録_2進法_問題B04

Last updated at Posted at 2025-03-02

概要

  • 競技プログラミングを始めてみた。
    せっかくなので1テーマごとに解法と学びをアウトプットしてみる。

取り組む内容

  • 鉄則本[https://atcoder.jp/contests/tessoku-book] に沿って実施する。
  • 鉄則本.jpg
  • 言語は Python
  • 今回テーマは「2進法」

問題文

2 進法表記で表された整数 N が与えられます。
N を 10 進法に変換した値を出力するプログラムを作成してください。

制約

N の長さは1 文字以上 8 文字以下N は 0 と 1 からなる
N の先頭の桁は 1 である

出力

答えを整数で出力してください。

解法

N = input()

answer = 0
length = len(N)

for i in range(length):
  if N[i] == "1":
    answer += 2 ** (length- i -1)
    
print(str(answer))

結果

  • AC
  • Runtime: 9 ms
  • メモリ:8572 KB KB

振り返り

思想

  • 2進数→10進数の変換は  $\sum_{i=1}^n$ 2 ** i ( if i桁目=1)

学び

  • 今回はシンプルだったので特段なし

感想

- 2進数を使った探索法あると思うので、そのための基礎を確認できたかなと思います

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?