IT/Programming / / 2022. 7. 8. 16:34

[백준, Python] 5355번 화성 수학

반응형
시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 128 MB 7541 4438 4010 60.914%

문제

겨울 방학에 달에 다녀온 상근이는 여름 방학 때는 화성에 갔다 올 예정이다. (3996번) 화성에서는 지구와는 조금 다른 연산자 @, %, #을 사용한다. @는 3을 곱하고, %는 5를 더하며, #는 7을 빼는 연산자이다. 따라서, 화성에서는 수학 식의 가장 앞에 수가 하나 있고, 그 다음에는 연산자가 있다.

입력

첫째 줄에 테스트 케이스의 개수 T가 주어진다. 다음 줄에는 화성 수학식이 한 줄에 하나씩 주어진다. 입력으로 주어지는 수는 정수이거나 소수 첫째 자리까지 주어지며, 0 이상 100 이하이다. 연산자는 최대 3개 주어진다.

출력

각 테스트 케이스에 대해서, 화성 수학식의 결과를 계산한 다음에, 소수점 둘째 자리까지 출력한다.


 

더보기
# 1차 입력값 처리 : marsCase[0] (0~100)
def chkInput(testCaseList=None):
    marsOpr = ["@", "%", "#"]

    # 연산자 개수 및 값 체크( 최대 3개 )
    for marsCase in testCaseList:
        caseLength = len(marsCase)
        # 연산자 3개 이상일 경우 Exception
        if caseLength > 4:
            raise Exception()
        # 화성 연산자가 없을 경우 Exception
        for index in range(1, caseLength):
            if marsCase[index] not in marsOpr:
                raise Exception()
        # marsCase[0] 0 ~ 100 아닐 경우 Exception
        if not (0 <= float(marsCase[0]) <= 100):
            raise Exception()


# 결과값 계산
def calOutput(testCaseList=None):
    funcResult = []
    for marsCase in testCaseList:
        caseTmp = float(marsCase[0])
        # @ = x3    % = +5    # = -7
        for index in range(1, len(marsCase)):
            if "@" == marsCase[index]:
                caseTmp *= 3
            elif "%" == marsCase[index]:
                caseTmp += 5
            elif "#" == marsCase[index]:
                caseTmp -= 7

        funcResult.append(format(caseTmp, ".2f"))

    return funcResult


try:
    caseCntInput = int(input())
    caseList = []
    result = []
    for index in range(0, caseCntInput):
        caseList.append(list(input().split()))

    chkInput(caseList)
    result = calOutput(caseList)

    for index in range(0, len(result)):
        print(result[index])

except Exception as e: print(e)
반응형
  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유