본문 바로가기
컴퓨터/백준 문제풀이

9037 The candy war, 16769 Mixing Milk, 2480 주사위 세개

by 하링아 2020. 10. 23.
반응형

9037 The candy war, 16769 Mixing Milk,  2480 주사위 세개

 

9037 The candy war

# 첫번째 풀이(리스트 컴프리헨션 이용 및 함수화 x)
T = int(input())
for _ in range(T):
	N = int(input())
	array = list(map(int, input().split()))
	if N == 1:
		print(0)
		continue	
	cycle = 0
	array = [i+1 if i%2 != 0 else i for i in array]
	if len(set(array)) == 1:
		print(0)
		continue
	while True:
		array = [i // 2 for i in array]
		temp = array[-1]
		for i in range(N-2, -1, -1):
			array[i+1] += array[i]
		array[0] += temp
		array = [i+1 if i%2 != 0 else i for i in array]
		cycle += 1
		if len(set(array)) == 1:
			print(cycle)
			break	

# 두번째 풀이(함수 생성)        
def check(N, candy):
	for i in range(N):
		if candy[i] % 2 == 1:
			candy[i] += 1
	return len(set(candy)) == 1
	
def teacher(N, candy):
	tmp_lst = [0 for i in range(N)]
	for idx in range(N):
		if candy[idx] % 2:
			candy[idx] += 1
		candy[idx] //= 2
		tmp_lst[(idx+1) % N] = candy[idx]
		
	for idx in range(N):
		candy[idx] += tmp_lst[idx]
		
	return candy
	
def process():
	N, candy = int(input()), list(map(int, input().split()))
	cnt = 0
	while not check(N, candy):
		cnt += 1
		candy = teacher(N, candy)
	print(cnt)
	
for i in range(int(input())):
	process()

 

 

16769 Mixing Milk

# 첫번째 풀이
def process(N, M):
	num1 = N[1] + M[1]
	if num1 > M[0]:
		M[1] = M[0]
		N[1] = num1 - M[0]
	else:
		M[1] = num1
		N[1] = 0
	
a = list(map(int, input().split()))
b = list(map(int, input().split()))
c = list(map(int, input().split()))		
	
for _ in range(33):
	process(a, b)
	process(b, c)
	process(c, a)
process(a, b)

print(a[1], b[1], c[1])

# 두번째 풀이
C, M = list(), list()

for i in range(3):
	a, b = map(int, input().split())
	C.append(a)
	M.append(b)
	
for i in range(100):
	idx = i % 3
	nxt = (idx + 1) % 3
	
	M[idx], M[nxt] = max(M[idx] - (C[nxt] - M[nxt]), 0), min(C[nxt], M[nxt] + M[idx])
	
for i in M:
	print(i)

- 값을 저장하는 방법에 따라서 쉽게 처리 가능 (다른 종류 값의 객체 여러개, 같은 종류의 값들을 같은 리스트에)

- 나머지를 이용해서 반복 인덱스 가능

 

 

2480 주사위 세개

# 첫번째 풀이
dice = list(map(int, input().split()))
if len(set(dice)) == 1:
	print(10000 + (set(dice).pop() * 1000))
elif len(set(dice)) == 3:
	print(100 * max(dice))
else:
	temp = [0, 0, 0, 0, 0, 0, 0]
	for i in dice:
		temp[i] += 1
	for i in range(len(temp)):
		if temp[i] == 2:
			print(1000 + (i*100))
            
# 두번째 풀이
dice = list(map(int, input().split()))
if len(set(dice)) == 1:
	print(10000 + (set(dice).pop() * 1000))
elif len(set(dice)) == 3:
	print(100 * max(dice))
else:
	print(1000 + sorted(dice)[1] * 100)
반응형

댓글