백준 16769번 <Mixing Milk>
백준 알고리즘 16769번
https://www.acmicpc.net/problem/16769
16769번: Mixing Milk
The first line of the input file contains two space-separated integers: the capacity $c_1$ of the first bucket, and the amount of milk $m_1$ in the first bucket. Both $c_1$ and $m_1$ are positive and at most 1 billion, with $c_1 \geq m_1$. The second and t
www.acmicpc.net
16769번
입력으로 3줄에 걸쳐 3종류의 우유가 담긴 병의 capacity와 실제 우유가 담긴 양이 들어온다.
출력으로는 3종류의 우유를 일련의 과정으로 섞었을 때, 3개의 병에 남은 양을 출력하면 된다.
일련의 과정
- 1번 병에서 2번병으로 우유를 옮긴다.
- 2번 병에서 3번병으로 우유를 옮긴다.
- 3번 병에서 1번병으로 우유를 옮긴다.
- 1번 병에서 2번병으로 우유를 옮긴다.
...... 100번 반복
문제 해결
a병에서 b병으로 우유를 옮기는 함수 pour()를 구현하였다.
우유를 옮기는 경우는 2가지로 나눌 수 있는데, 첫번째는 a병의 우유를 b병으로 모두 옮길 수 있는 경우이고 두번째는 a병의 우유를 b병으로 다 옮기지 못하는 경우이다.
이를 고려하여 pour()를 구현하여야 한다.
100번 우유를 섞는 mixing 함수를 구현하였다.
처음에 문제를 이해하기로 1->2, 2->3, 3->1, 1->2 이 과정을 100번 하는 줄 알았는데 그게 아니었다.
1->2 가 1번에 속하는 거였다.
코드
def pour(a, b):
temp = a[0] + b[0]
if temp > b[1]:
b = (b[1], b[1])
a = (temp-b[1], a[1])
else:
b = (temp, b[1])
a = (0, a[1])
return a, b
def mixing(one, two, three):
for _ in range(33):
one, two = pour(one, two)
two, three = pour(two, three)
three, one = pour(three, one)
one, two = pour(one, two)
return one, two, three
a, b = map(int, input().split())
one = (b, a)
a, b = map(int, input().split())
two = (b, a)
a, b = map(int, input().split())
three = (b, a)
one, two, three = mixing(one, two, three)
print(one[0])
print(two[0])
print(three[0])
결과