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をPythonで取り組んだ振り返り(ABC427 A~B)

Posted at

どうもこんにちは!

今週のコンテストはBまで完答、振り返りもBまで。残念ながら2回連続でC問題解けず。
Cはグラフから辺を削除して2部グラフにするという問題でした。
性質的に辺の数が奇数の閉路から辺を除去すればよいところまでは考えたのですが、辺の選択方法がわからず解けませんでした。
解説を見ると、色の塗り方を全探索すればよかったようですね。

問題と公式の解説のリンク

問題は以下のリンクから。

A - ABC -> AC -

問題

与えられた文字列の中央の文字(長さをLとすると(L+1)/2文字目)を削除した文字列を出力する問題。

考え方とコード

文字列のスライスを使って該当部分だけ抜いた文字列を作って出力しました。

n = input()
l = len(n)
print(n[:(l+1)//2-1]+n[(l+1)//2:])

B - Sum of Digits Sequence -

問題

f(x)をxの各桁の和とします。例えばf(12) = 3です。(1+2で3)
このとき、$A_i = \sum^{i-1}_{j=0}f(A_j)$ を出力する問題。ただし$A_0 = 1$とします。

考え方とコード

$A_i$の値と$f(A_i)$を保存するリストを用意し、$A_i$を求めるときに$A_{i-1}$を文字列化して各桁を足し合わせて$A_i$の値を加算してリストに保存、最後の値を出力するとしました。

n = int(input())

a = [1,1,2]
ans = 2
for i in range(3,n+1):
    s = str(a[i-1])
    tmp = 0
    for j in s:
        tmp += int(j)
    ans += tmp
    a.append(ans)

print(a[n])

ではでは。

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?