728x90
✏️ 문제
https://www.acmicpc.net/problem/2292
✏️ 풀이방법
1. 중앙의 방 1 부터 시작해서 각 면은 6의 배수로 증가한다. 1(+6),7(+12),19등의 규칙으로 증가
2. 입력한 수가 각 방의 최대의 숫자 안으로 들어오는 경우로 몇개의 방을 지나가는지 찾는다.
📌 코드
n = int(input())
beehouse = 1
i = 1
while True:
if n <= beehouse:
break
beehouse += (6 * i)
i += 1
print(i)
📌 결과
13 #입력
3 #출력
🤔 시행착오.1
n = int(input())
beehouse = []
room = []
for i in range(1,1+1):
room.append(i)
beehouse.append(room)
room = []
for i in range(2,7+1):
room.append(i)
beehouse.append(room)
room = []
for i in range(8,19+1):
room.append(i)
beehouse.append(room)
print(beehouse)
n = int(input())
beehouse = [[1]]
# while True:
room = []
for i in range(beehouse[0][-1]+1,beehouse[0][-1]+(6*(2-1))+1):
room.append(i)
beehouse.append(room)
room = []
for i in range(beehouse[1][-1]+1,beehouse[1][-1]+(6*(3-1))+1):
room.append(i)
beehouse.append(room)
print(beehouse)
n = int(input())
beehouse = [[1]]
i=0
while True:
room = []
for num in range(beehouse[i][-1]+1,beehouse[i][-1]+((6*(i+1))+1),1):
room.append(num)
beehouse.append(room)
i += 1
if n in beehouse[i]:
break
print(len(beehouse))
🤔 시행착오.2
n = int(input())
beehouse = 1
room = 1
i=1
if (1+6*(i-2)) < n and n <= (1+6*(i-1)):
i=2
if (1+6*(i-2)) < n and n <= 7(1+6*(i-1)):
i=3
if (1+6*(i-2)) < n and n <= 19(1+6*(i-1)):
print(i)
n = int(input())
beehouse = 1
while True:
if (1 + 6 * (beehouse - 2)) < n and n <= (1 + 6 * (beehouse - 1)):
break
else:
beehouse+=1
print(beehouse)
-
🤔 시행착오.3
n = int(input())
beehouse = 0
total = sum(beehouse)
while True:
if n <= 6 * total:
break
beehouse += 1
print(beehouse)
print(total)
TypeError: 'int' object is not iterable
- int는 sum() 함수를 사용할 수 없다
🤔 시행착오.4
n = int(input())
beehouse = 0
i = 0
while True:
beehouse += (6*i)
i+=1
if n <= beehouse:
break
print(i)
- 7을 입력했을 때 2가 나와야하는 3출력
📝 메모
- 소인수분해 문제와 같이 모든 조건을 다 찾으면서 풀면 런타임에러나 시간초과나 날 수 있음을 고려할 것
'Python > online judge' 카테고리의 다른 글
백준 [1181.단어 정렬] | Python (0) | 2024.01.15 |
---|---|
백준 [2745.진법 변환] | Python (0) | 2024.01.12 |
백준 [2750.수 정렬하기] | Python (0) | 2024.01.10 |
백준 [11653.소인수분해] | Python (0) | 2024.01.10 |
백준 [2581.소수] | Python (0) | 2024.01.10 |