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 ABC問題 #043A、044A 自分用メモ

Posted at

#043A - キャンディーとN人の子供イージー 問題文

解き方

Sample.py
import sys

def main():
    input = sys.stdin.readline
    nm = int(input())
    ans = int(nm * (nm + 1) / 2)
    print(ans)

if __name__ == "__main__":
    main()

こちらは等差数列の和の公式を使いました。
和の公式日常生活で使うことはあまりないので、だいぶ忘れていましたが・・・公式さえ使えば単純な計算処理でした。

▼等差数列の和の公式

1+2+⋯+N=N(N+1)/2

公式のユーザ解説をみると、for文で和を求めていくやり方も紹介されていました。

#044A - 高橋君とホテルイージー 問題文

初めに考えた処理は以下。

Sample.py
import sys

def main():
    input = sys.stdin.readline
    n = int(input())
    k = int(input())
    x = int(input())
    y = int(input())
    
    p = n - k
    
    if p > 0:
        ans = (k * x) + (p * y)
    elif p <= 0:
         ans = n * x
        
    print(ans)
    
if __name__ == "__main__":
    main()

ChatGPTにレビューしてもらうと、

① N K X Y は 1行で記載:
 n, k, x, y = map(int, input().split())

② min/max で分けるのが読みやすい:
ans = min(n, k) * x + max(0, n - k) * y

との指摘がありました。

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?