3
0

はじめに

ABC345です!3完でした :sob:

A - Leftrightarrow

問題

考えたこと

前と後ろにのみ"<"と">"があって、"="が1つ以上あれば"Yes"、それ以外は"No"です。

ACコード

a.py
s = input()

if (
    s[0] == "<"
    and s[-1] == ">"
    and s.count("<") == 1
    and s.count(">") == 1
    and s.count("=") > 0
):
    print("Yes")
else:
    print("No")

一行で書けそうだったので挑戦してみました。中身は同じです。

a_oneline.py
print("YNeos"[not ((s := input())[0] == "<" and s[-1] == ">" and s.count("<") == 1 and s.count(">") == 1 and s.count("=") > 0) :: 2])

B - Integer Division Returns

問題

考えたこと

decimalを使いました。

ACコード

b.py
from math import ceil
from decimal import Decimal

x = int(input())
print(ceil(Decimal(x) / Decimal(10)))

C - One Time Swap

問題

考えたこと

重複する文字がない場合、注目している文字以外を数え上げます。(図1)

counting.png

重複する文字がある場合、単純に重複する文字を数え上げないという方針で行うと、初期状態の文字列を数え忘れるので注意が必要です。

counting_dup.png

ACコード

c.py
from collections import defaultdict

s = input()
n = len(s)
d = defaultdict(int)

ans = 0
for i in range(n):
    d[s[i]] += 1
    if d[s[i]] > 1:
        ans = 1  # ここで重複したときの数え上げ忘れ分を調整

for i in range(n):
    d[s[i]] -= 1
    ans += (n - i - 1) - d[s[i]]

print(ans)

感想

精度が求められるときいつもdecimalを使って解いているような・・・pypyのdecimalは遅いらしくそろそろこの辺りを勉強したい(しない)

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