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 Beginner Contest 174をPythonで解く

0
Posted at

AtCoder Beginner Contest 174の問題をPythonで解いてみました。

A - Air Conditioner

入力値が$30$以上かどうかをif文で判定します。

main.py (A - Air Conditioner)
if int(input()) >= 30:
  print("Yes")
else:
  print("No")

B - Distance

問題文にある条件を順に確認していきます。

main.py (B - Distance)
n, d = map(int, input().split())

ans = 0
for i in range(n):
    x, y = map(int, input().split())
    if (x**2 + y**2) ** 0.5 <= d:
        ans += 1

print(ans)

C - Repsept

$7$、$77$、$777$、……の数列を$\{ a_i \}$とすると, これは漸化式で以下のように表されます。

  a_i = \left\{
  \begin{aligned}
    & 7 & (i = 1) \\
    & 10 \times a_{i-1} + 7 & (i \geq 2)
  \end{aligned}
  \right.

$K$で割り切れるかが重要なので、$\mathrm{mod} \ K$を考えることにします。

ある$i$で以前に出てきた値が再度出現すると、それ以降は出現パターンが再度繰り返されます。$\mathrm{mod} \ K$としてとれる値は$0$から$K-1$までの$K$通りしかありませんので、鳩の巣原理より高々$i = K$までの調査でよいことになります。

main.py (C - Repsept)
k = int(input())

a = 0
for i in range(k):
    a = (10 * a + 7) % k
    if a == 0:
        print(i + 1)
        exit()

print(-1)

D - Alter Altar

「赤い石の左隣に置かれた白い石は災いを招く」というのは、すなわち左側にR、右側にWがまとまって存在するようにしなければならないことになります。
イメージとしては、左の方にあるWを右の方にあるRと入れ替えればよいことになります。

R...R | W .. W

次のようにすれば、最小の手順で災いを招かない状態にできます。

  1. $c$に存在するRの個数(=r_count)を調べます。
  2. 左端からr_count個を「左側」、それ以外を「右側」と呼びます。
  3. 左側に含まれるWと右側に含まれるRを入れ替えます。

つまり、左側にあるWの個数を出力すればOKです。

main.py (D - Alter Altar)
n = int(input())
c = input()
print(c[: c.count("R")].count("W"))

参考にしたサイト等

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?