새소식

⌨️ Algorithms/백준

[Python] 백준 13985번_Equality

2023. 6. 21. 19:24

  • -

https://www.acmicpc.net/problem/13985

 

13985번: Equality

Print, on a single line, YES if the sum is correct; otherwise, print NO.

www.acmicpc.net

 

  • The quiz asks a student for the sum of the numbers
  • Determine if the student taking the quiz got the question correct
  • 입력
    • The first and the only line of input contains a string of the form: a + b = c
    • It is guaranteed that a, b, and c are single-digit positive integers
    • The input line will have exactly 9 characters, formatted exactly as shown, with a single space separating each number and arithmetic operator
  • 출력
    • Print, on a single line, YES if the sum is correct; otherwise, print NO
  • 시간 제한: 1초
  • 메모리 제한: 512 MB

 

"""
입출력 예시)

(예제 입력 1) 
1 + 2 = 3
            -> YES

(예제 입력 2) 
2 + 2 = 5
            -> NO
"""

 

 

## 의사코드 ##

# a, b, c는 모두 한 자리 양의 정수

# a + b = c 이면 yes 
# 아니면 no 

# '='으로 split해서 eval() 함수로 계산한 값이 같은지 확인
# string = input().split('=')
# if eval(string[0]) == int(string[1]):

 

 

 

 

string = input().split('=')
if eval(string[0]) == int(string[1]):
    print('YES')
else:
    print('NO')

 

 

  • eval()
    • 문자로 표현된 표현식의 값을 return 
      • ex) 
        • eval('1 + 2') 
        • => 3

 

 

 

 

 

 

https://hbase.tistory.com/397

 

[Python] 파이썬 eval() 함수 사용법 및 예제

파이썬은 eval()이라는 함수를 지원한다. 파이썬의 eval() 함수는 문자열로 표현되는 표현식(expression)을 실행해서 결과값을 받아오는 함수다. 예를 들어보자. exp = "1 + 2" result = eval(exp) print(result) # 3

hbase.tistory.com

 

https://developer-next-to-you.tistory.com/245

 

[Python] 백준 13985번(Equality) 문제 풀이

s = input().split(" = ") if eval(s[0]) == int(s[1]): print("YES") else: print("NO")

developer-next-to-you.tistory.com

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다!