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?

問題文 045A

Sample.py

import sys

def main():
    input = sys.stdin.readline
    a = int(input())
    b = int(input())
    h = int(input())
    
    ans = (a + b) * h / 2
        
    print(int(ans))
    
if __name__ == "__main__":
    main()

単純に台形の公式に当てはめて解く。

問題文 046A

Sample.py

import sys
def main():
    input = sys.stdin.readline
    a = int(input())
    b = int(input())
    c = int(input())
    
    print(len({a, b, c}))
    
if __name__ == "__main__":
    main()

・len()でリストの要素数取得
・重複を省く時はsetが定石

問題文 047A

Sample.py
import sys

def main():
    input = sys.stdin.readline
    a,b,c = map(int,input().split())
    
    if a + b == c:
        print("Yes")
    elif a + c == b:
        print("Yes")
    elif b + c == a:
        print("Yes")
    else:
        print("No")
    
if __name__ == "__main__":
    main()

問題文 048A

サクサクです。

Sample.py

import sys

def main():
    input = sys.stdin.readline
    s = input()
    
    print("A" ,end = "")
    print(s[8] ,end = "")
    print("C" ,end = "")
    
if __name__ == "__main__":
    main()

問題文 049A

Sample.py
import sys

def main():
    input = sys.stdin.readline
    c = input().strip()
    li = {"a","e","i","o","u"}
    
    if c in li:
        print("vowel")
    else:
        print("consonant")
    
if __name__ == "__main__":
    main()

sys.stdin.readlineを使うと、改行も読み込まれるのでWAの原因になる。
input().strip()を使用するか、単純なinput()を使用することで解決します。

問題文 050A

Sample.py
import sys

def main():
    input = sys.stdin.readline
    a,op,c = input().split()
    
    if op == "+":
        print(int(a) + int(c))
    else:
        print(int(a) - int(c))
    
if __name__ == "__main__":
    main()

Gemini「競技プログラミングのこの手の問題(数式が文字列で与えられる)では、Pythonの eval() 関数を使うと1行で解けます。」
私「そうなの!?」

別解

Sample.py
import sys

def main():
    input = sys.stdin.readline
     = input()
    print(eval(s))
   
if __name__ == "__main__":
    main()

ほんとにACなった!感動です。

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?