백준 1330번 두수 비교하기

[파이썬/Python] 백준 1330번 두 수 비교하기

 

[파이썬/Python] 백준 1330번 두 수 비교하기

풀이a, b = map(int,input().split())if a > b: print('>')elif a

parkyongrok.tistory.com

 

 

 

기존 문제 풀이는 다음과 같다.

a, b = map(int,input().split())

if a > b:
    print('>')
elif a < b:
    print('<')
else:
    print('==')

 

위의 문제를 다음과 같이 함수화 하였다

# 백준 1330번 두 수 비교하기
def compare_two_number(first_num, second_num):
    if first_num > second_num:
        return ">"
    elif first_num < second_num:
        return "<"
    else:
        return '=='
    
a, b = map(int,input().split())
print(compare_two_number(a,b))

 

백준 9498번 시험성적

[파이썬/Python] 백준 9498번 시험성적

 

[파이썬/Python] 백준 9498번 시험성적

풀이score = int(input())if score >= 90: grade = 'A'elif score >= 80: grade = 'B'elif score >= 70: grade = 'C'elif score >= 60: grade = 'D'else: grade = 'F' print(grade)

parkyongrok.tistory.com

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

 

기존 풀이는 다음과 같다

score = int(input())

if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
elif score >= 70:
    grade = 'C'
elif score >= 60:
    grade = 'D'
else:
    grade = 'F'
    
print(grade)

 

위의 문제를 다음과 같이 함수화하였다.

# 백준 9498번 시험성적
def return_test_grade(score):
    if score >= 90:
        return 'A'
    elif score >= 80:
        return 'B'
    elif score >= 70:
        return 'C'
    elif score >= 60:
        return'D'
    else:
        return 'F'
    
score = int(input())
print(return_test_grade(score))

 

백준 2753번 윤년

[파이썬/Python] 백준 2753번 윤년

 

[파이썬/Python] 백준 2753번 윤년

풀이year = int(input())if year%400==0: print(1)elif year%4==0 and not year%100==0: print(1)else: print(0)> 윤년이 되는 조건에 대해 잘 생각해 보아야 한다. 우선, 400의 배수인 해는 무조건적으로 윤년이므로 우선적으

parkyongrok.tistory.com

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

 

기존 풀이는 다음과 같다.

year = int(input())

if year%400==0:
  print(1)
elif year%4==0 and not year%100==0:
  print(1)
else:
  print(0)

 

위의 문제를 다음과 같이 함수화 하였다.

# 백준 2753번 윤년
def is_leapyear(year):
    if year%400==0:
        return True
    elif year%4==0 and not year%100==0:
        return True
    else:
        return False
    
year = int(input())
print(is_leapyear(year))

백준 14681번 사분면 고르기

[파이썬/Python] 백준 14681번 사분면 고르기

 

[파이썬/Python] 백준 14681번 사분면 고르기

풀이x 좌표가 양수인지 음수인지 확인y 좌표가 양수인지 음수인지 확인해당하는 사분면 출력x = int(input())y = int(input())if x > 0 : if y > 0 : print(1) else: print(4)else: if y > 0: print(2) else: print(3)

parkyongrok.tistory.com

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

 

기존 풀이는 다음과 같다.

x = int(input())
y = int(input())

if x > 0 :
  if y > 0 :
    print(1)
  else:
    print(4)
else:
  if y > 0:
    print(2)
  else:
    print(3)

 

위의 문제를 다음과 같이 함수화 하였다.

# 백준 14681번 사분면 고르기
def return_quadrant(x, y):
    if x > 0 :
        if y > 0 :
            return 1
        else:
            return 4
    else:
        if y > 0:
            return 2
        else:
            return 3

x = int(input())
y = int(input())
print(return_quadrant(x,y))

백준 2884번 알람 시계

[파이썬/Python] 백준 2884번 알람 시계

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

 

기존 풀이는 다음과 같다.

H, M = map(int, input().split())

if M-45 < 0:
    H = (H-1)%24
    M = (M-45)%60
else:
    M = (M-45)%60

print(H,M)

 

위의 문제를 다음과 같이 함수화 하였다.

# 백준 2884번 알람 시계
def set_alarmclock(H,M):
    if M-45 < 0:
        H = (H-1)%24
        M = (M-45)%60
    else:
        M = (M-45)%60
    return H, M

Hour, Minute = map(int, input().split())
print(set_alarmclock(Hour, Minute))

 


 

백준 2525번 오븐 시계

[파이썬/Python] 백준 2525번 오븐 시계

 

[파이썬/Python] 백준 2525번 오븐 시계

풀이A, B = map(int,input().split())C = int(input())B += CA = A + (B//60)A = A % 24B = B % 60print(A,B)

parkyongrok.tistory.com

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

 

기존 풀이는 다음과 같다.

A, B = map(int,input().split())
C = int(input())

B += C
A = A + (B//60)

A = A % 24
B = B % 60

print(A,B)

 

위의 문제를 다음과 같이 함수화 하였다

# 백준 2525번 오븐 시계
def set_ovenclock(Hour, Minute, cooking_time):
    Minute += cooking_time
    Hour = Hour + (Minute//60)

    return Hour % 24, Minute % 60

A, B = map(int,input().split())
C = int(input())
print(set_ovenclock(A, B, C))

 


백준 2480번 주사위

[파이썬/Python] 백준 2480번 주사위

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

 

기존 풀이는 다음과 같다.

a, b, c = map(int,input().split())

if a == b == c :
  print(10000+(a*1000))
elif a==b or b==c or a==c:
  if a==b:
    print(1000+(a*100))
  elif b==c:
    print(1000+(b*100))
  else:
    print(1000+(c*100))
else:
  print(max(a,b,c)*100)

 

위 풀이를 다음과 같이 함수화 하였다

# 백준 2480번 주사위

def prize_dice(a ,b ,c):
    if a == b == c :
      return 10000+(a*1000)
    elif a==b or b==c or a==c:
       if a==b:
          return 1000+(a*100)
       elif b==c:
          return 1000+(b*100)
       else:
          return 1000+(c*100)
    else:
       return max(a,b,c)*100
    
a, b, c = map(int,input().split())
print(prize_dice(a,b,c))

 

 

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


풀이
a, b, c = map(int,input().split())

if a == b == c :
  print(10000+(a*1000))
elif a==b or b==c or a==c:
  if a==b:
    print(1000+(a*100))
  elif b==c:
    print(1000+(b*100))
  else:
    print(1000+(c*100))
else:
  print(max(a,b,c)*100)

 

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


풀이
A, B = map(int,input().split())
C = int(input())

B += C
A = A + (B//60)

A = A % 24
B = B % 60

print(A,B)

 

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


풀이
H, M = map(int, input().split())

if M < 45:
  if H == 0:
    H = 23
    M += 15
  else:
    H -= 1
    M += 15
else:
  M -= 45

print(H,M)

 

> -15%60 = 45 같이 음수롤 mod 연산하면 양수가 나온다(나머지는 항상 0보다 크거나 같아야 한다)

위의 연산적 특징을 이용하면 코드를 더욱 간결하게 줄일 수 있다.

H, M = map(int, input().split())

if M-45 < 0:
    H = (H-1)%24
    M = (M-45)%60
else:
    M = (M-45)%60

print(H,M)

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


풀이

x 좌표가 양수인지 음수인지 확인

y 좌표가 양수인지 음수인지 확인

해당하는 사분면 출력

x = int(input())
y = int(input())

if x > 0 :
  if y > 0 :
    print(1)
  else:
    print(4)
else:
  if y > 0:
    print(2)
  else:
    print(3)

 

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


풀이
year = int(input())

if year%400==0:
  print(1)
elif year%4==0 and not year%100==0:
  print(1)
else:
  print(0)

> 윤년이 되는 조건에 대해 잘 생각해 보아야 한다.

 우선, 400의 배수인 해는 무조건적으로 윤년이므로 우선적으로 걸러낸다.

 그 후, 4의 배수이며 100의 배수가 아닌 해를 윤년으로 걸러낸다.

 나머지는 평년이다.

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


풀이
score = int(input())

if score >= 90:
    grade = 'A'
elif score >= 80:
    grade = 'B'
elif score >= 70:
    grade = 'C'
elif score >= 60:
    grade = 'D'
else:
    grade = 'F'
    
print(grade)

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


풀이
a, b = map(int,input().split())

if a > b:
    print('>')
elif a < b:
    print('<')
else:
    print('==')

+ Recent posts