조건문
1330 - 방법1
a, b = map(int, input().split())
if a>b :
print(">")
elif a<b :
print("<")
else :
print("==")
1330 - 방법2
a, b = map(int, input().split())
c = lambda a, b : print(">") if a>b else( print("<") if a<b else print("==") )
c(a,b)
방법1
1.map(int, input().split())
로 사용자로부터 공백을 기준split()
으로 입력을 받아서 map()
으로 하나씩 돌며 int
형으로 형변환
2.조건문을 활용해서 if, elif, else
구문 구현
3.출력
방법2
1.1.map(int, input().split())
로 사용자로부터 공백을 기준split()
으로 입력을 받아서 map()
으로 하나씩 돌며 int
형으로 형변환
2.람다함수를 이용해서 조건문을 만들었다.
lambda 매개변수 : 결과
lambda 매개변수 : 결과 if 조건 else 결과
lambda 매개변수 : 결과 if 조건 else (결과 if 조건 else 결과)
3.람다함수 호출
함수를 변수에 할당 후 변수로 함수 호출 c(a,b)
9498번
방법1
a = int(input())
if a >= 90 :
print("A")
elif a >= 80 :
print("B")
elif a >= 70 :
print("C")
elif a >= 60 :
print("D")
else:
print("F")
방법2
a = int(input())
b = lambda a : print("A") if a>=90 else(print("B") if a>=80 else(print("C") if a>=70 else(print("D") if a>=60 else print("f"))))
b(a)
방법1
1.if elif else
문 활용한 조건문 만들기.
방법2
1.lambda를 이용해서 풀어보았다.
2.하지만 중첩으로 if - else 구문을 작성하는게 가독성이 너무 않좋다.
3.시험삼아 풀어보았을 뿐 방법1이 더 가독성과 직관적으로 코드를 알 수 있는 것 같다.
2753번
a = int(input())
if a%4 == 0 and a%100 != 0 or a%4 == 0 and a%400 == 0:
print("1")
else :
print("0")
1.윤년을 구한는 문제이다.
2.조건
and
100의 배수가 아닐경우 or
연산자로 두 조건을 연결 하나가 True
이면 True
and
400의 배수 일때3.and
와or
논리연산자를 이용해서 if
문에서 한번에 확인
14681번
x = int(input())
y = int(input())
def quadrant(x,y):
if(x > 0 and y >0):
print("1")
elif(x<0 and y >0 ):
print("2")
elif (x<0 and y<0):
print("3")
else:
print("4")
quadrant(x, y)
함수로 정의해서 풀어보았다.
함수에 사용자로 부터 입력받은 데이터를 매개변수로 받아서 함수 안에서
조건을 판단하는 코드를 구현했다.
if-elif-else
구문은 위의 코드와 같다.
2884번 방법1
h, m = map(int, input().split())
h = h*60
time = h + m
result = time - 45
if result//60 >= 0 :
print(result//60,result%60)
else:
print((result//60) +24, result%60)
2884번 방법2
a,b=map(int,input().split())
c=(a*60+b-45)%1440
print(c//60,c%60)
방법1
1.입력받은 수를 분으로 만든 후 -45를 한다.
2.if-else
으로 조건에 따라 다르게 구현하게 했다.
3. 제대로 된 코드는 아니라고 생각한다.
방법2
1.두 수를 입력박아 분으로 만든 후 나머지를 구하는 식으로 코드를 구현
음수의 나머지 연산에 대해서 알아보자
2525번
hour, minute = map(int,input().split())
need_time = int(input())
current_time = hour*60 + minute
finish_time = current_time + need_time
finsih_time = finish_time%1440
print(finsih_time//60,finish_time%60)
1.전체시간을 전체 분으로 나누어서 계산한다.
2480번
a, b, c = map(int , input().split())
if a == b and b == c and c == a:
print(10000 + a * 1000)
elif a == b and b != c and a != c:
print(1000 + a*100)
elif a != b and b == c and c != a:
print(1000 + b*100)
elif a != b and b != c and c == a:
print(1000 + c*100)
else :
print(max(a,b,c) *100)
a,b,c를 입력받아서 5가지 조건문을 만들었다.