반응형

문제

Farming is competitive business -- particularly milk production. Farmer John figures that if he doesn't innovate in his milk production methods, his dairy business could get creamed!

Fortunately, Farmer John has a good idea. His three prize dairy cows Bessie, Elsie, and Mildred each produce milk with a slightly different taste, and he plans to mix these together to get the perfect blend of flavors.

To mix the three different milks, he takes three buckets containing milk from the three cows. The buckets may have different sizes, and may not be completely full. He then pours bucket 1 into bucket 2, then bucket 2 into bucket 3, then bucket 3 into bucket 1, then bucket 1 into bucket 2, and so on in a cyclic fashion, for a total of 100 pour operations (so the 100th pour would be from bucket 1 into bucket 2). When Farmer John pours from bucket a into bucket b, he pours as much milk as possible until either bucket a becomes empty or bucket b becomes full.

Please tell Farmer John how much milk will be in each bucket after he finishes all 100 pours.

입력

The first line of the input file contains two space-separated integers: the capacity c1 of the first bucket, and the amount of milk m1 in the first bucket. Both c1 and m1 are positive and at most 1 billion, with c1≥m1. The second and third lines are similar, containing capacities and milk amounts for the second and third buckets.

출력

Please print three lines of output, giving the final amount of milk in each bucket, after 100 pour operations.

문제의 예제를 보자면 각각의 크기가 10L, 11L, 12L 인 양동이 1,2,3번이 있고

초기에 들어있는 우유의 양은 각각 3L, 4L, 5L라고 한다. 

1) 1번을 2번에 붓는다. 만약 2번이 넘칠 시 그만 붓는다.

2) 2번을 23번에 붓는다. 만약 3번이 넘칠 시 그만 붓는다.

3) 3번을 1번에 붓는다. 만약 1번이 넘칠 시 그만 붓는다.

...

100) ....

이런식으로 진행 되며 처음의 직관적인 코드는 문제 보자마자 생각나는데로 적어보았다. 만약 양동이의 갯수가 3개가 아니고 100개 혹은 1000개일경우 이런식으 코드는 너무나 비효율적이기 때문에 각각의 회차를 for문에서 int i로 기억을 하고 i++을 하여 i%3의 값에따라 계산을 할 수 있게 수정하였다.

 

<직관적인 코드>

#include<stdio.h>
using namespace std;

int capacity[3];
int milk[3];

int main(void)
{
	for (int i = 0; i < 3; i++)
	{
		scanf("%d %d", &capacity[i], &milk[i]);
	}

	for (int i = 0; i < 100; i++)
	{
		if (i % 3 == 0)
		{
			if (milk[0] + milk[1] > capacity[1])
			{
				milk[0] = milk[0] + milk[1] - capacity[1];
				milk[1] = capacity[1];
			}
			else
			{
				milk[1] = milk[0] + milk[1];
				milk[0] = 0;
			}
		}
		else if (i % 3 == 1)
		{
			if (milk[1] + milk[2] > capacity[2])
			{
				milk[1] = milk[1] + milk[2] - capacity[2];
				milk[2] = capacity[2];
			}
			else
			{
				milk[2] = milk[1] + milk[2];
				milk[1] = 0;
			}
		}
		else // (i % 3 == 2)
		{
			if (milk[2] + milk[0] > capacity[0])
			{
				milk[2] = milk[2] + milk[0] - capacity[0];
				milk[0] = capacity[0];
			}
			else
			{
				milk[0] = milk[2] + milk[0];
				milk[2] = 0;
			}
		}
	}

	for (int i = 0; i < 3; i++)
	{
		printf("%d\n", milk[i]);

	}
}

 

 

<반복되는 코드를 압축하여 수정>

#include<stdio.h>
using namespace std;

int capacity[3];
int milk[3];

int main(void)
{
	for (int i = 0; i < 3; i++)
	{
		scanf("%d %d", &capacity[i], &milk[i]);
	}

	for (int i = 0; i < 100; i++)
	{

		if (milk[i % 3] + milk[(i + 1) % 3] > capacity[(i + 1) % 3])
		{
			milk[i % 3] = milk[i % 3] + milk[(i + 1) % 3] - capacity[(i + 1) % 3];
			milk[(i + 1) % 3] = capacity[(i + 1) % 3];
		}
		else
		{
			milk[(i + 1) % 3] = milk[i % 3] + milk[(i + 1) % 3];
			milk[i % 3] = 0;
		}

	}

	for (int i = 0; i < 3; i++)
	{
		printf("%d\n", milk[i]);

	}
}

 

 

추가적으로 <Python 풀이>도...

c1, m1 = map(int, input().split())
c2, m2 = map(int, input().split())
c3, m3 = map(int, input().split())

capacity = [c1, c2, c3]
milk = [m1, m2, m3]

for i in range(100):
    a = milk[i % 3]
    b = milk[(i+1) % 3]
    if milk[i % 3] + milk[(i+1) % 3] > capacity[(i+1) % 3]:

        milk[i % 3] = a + b - capacity[(i+1) % 3]

        milk[(i + 1) % 3] = capacity[(i+1) % 3]

    else:
        milk[i % 3] = 0

        milk[(i + 1) % 3] = a + b

for i in range(3):
    print(milk[i])

 

 

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

 

반응형

+ Recent posts