🌞코딩테스트/🧡백준 [단계별로 풀어보기]
[단계별로 풀어보기] 4. while문
hyerimmy
2021. 8. 13. 01:19
📂단계4 while문
📌while문을 사용해 봅시다.
https://www.acmicpc.net/step/4
입출력과 사칙연산 단계
입출력과 사칙연산
www.acmicpc.net
1. [10952] A+B-5 https://www.acmicpc.net/problem/10952
### [10952] A+B-5 ###
### https://www.acmicpc.net/problem/10952 ###
# 12:36 ~ 12:42 (6m)
import sys
a,b = map(int, sys.stdin.readline().split()) # 입력받기
while(a!=0 and b!=0):
print(a+b) # 합 출력하기
a,b = map(int, sys.stdin.readline().split()) # 입력받기
# 맞았습니다!!
2. [10951] A+B-4 https://www.acmicpc.net/problem/10951
### 백준 [10951] A+B-4 ###
### https://www.acmicpc.net/problem/10951 ###
# 12:44 ~ 12:46 (2m)
import sys
a,b = map(int, sys.stdin.readline().split()) # 입력받기
while (0 < a and b < 10):
print(a+b) # 합 출력하기
a,b = map(int, sys.stdin.readline().split()) # 입력받기
# 런타임 에러 발생
# 문제 의도 파악 어렵다
### 백준 [10951] A+B-4 ###
### https://www.acmicpc.net/problem/10951 ###
# 12:44 ~ 12:46
# 01:12 ~ 01:16 (6m)
import sys
while 1:
try:
a,b = map(int, sys.stdin.readline().split()) # 입력받기
print(a+b) # 합 출력하기
except:
break
# 맞았습니다!!
while 1: (무한반복구문) -> break문 필요
테스트케이스 수가 정해지지 않은 경우 -> try~except 문 활용
3. [1110] 더하기 사이클 https://www.acmicpc.net/problem/1110
### 백준 [1110] 더하기 사이클 ###
### https://www.acmicpc.net/problem/1110 ###
# 12:49 ~ 01:09 (10m)
import sys
# 결과값 저장할 변수
r = 0
result = []
# 입력받기
n = int(sys.stdin.readline())
result.append(n)
while 1:
# 단계1 - 각 자리수 더하기
r = result[-1] % 10 + result[-1]//10
# 단계2 - 새로운 수 구하기
r = result[-1]%10*10 + r%10
if r not in result:
result.append(r)
else:
break
print(len(result))
# 맞았습니다!!
리스트의 마지막 값 사용하기 -> list[-1]