백준 16769번 Mixing Milk
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | #include <iostream> #include <string> #include<queue> #include <vector> #include <algorithm> #include<cmath> using namespace std; int BucketSize[3]; int realBucket[3]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); for (int i = 0; i < 3; i++) { cin >> BucketSize[i] >> realBucket[i]; } int Count = 1; for (int a = 0; a < 99; a++) { for (int i = 0; i < 3; i++) { if (realBucket[i] + realBucket[(i + 1)%3] <= BucketSize[(i + 1)%3]) { realBucket[(i + 1)%3] += realBucket[i]; realBucket[i] = 0; } else { int MoveM = BucketSize[(i + 1)%3] - realBucket[(i + 1)%3]; realBucket[i] -= MoveM; realBucket[(i + 1)%3] += MoveM; } } } int i = 0; if (realBucket[i] + realBucket[i + 1] <= BucketSize[i + 1]) { realBucket[i + 1] += realBucket[i]; realBucket[i] = 0; } else { int MoveM = BucketSize[i + 1] - realBucket[i + 1]; realBucket[i] -= MoveM; realBucket[i + 1] += MoveM; } for (int i = 0; i < 3; i++) cout << realBucket[i] << endl; return 0; } | cs |
출처:https://www.acmicpc.net/problem/16769
좀더 간단하게 풀 수 있는 방법이 있을것 같은데 코드가 너무 길어졌네요.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | #include <iostream> #include <string> #include<queue> #include <vector> #include <algorithm> #include<cmath> using namespace std; int BucketSize[3]; int realBucket[3]; int main() { ios_base::sync_with_stdio(0); cin.tie(0); for (int i = 0; i < 3; i++) { cin >> BucketSize[i] >> realBucket[i]; } for (int i = 0, j = 0; j < 100; j++) { int next = (i + 1) % 3; int moveMilk = min(BucketSize[next] - realBucket[next], realBucket[i]); realBucket[next] += moveMilk; realBucket[i] -= moveMilk; i = next; } for (int i = 0; i < 3; i++) cout << realBucket[i] << endl; return 0; } | cs |
같이 연습문제를 푼 학회분 코드를 참조해서 코드를 깔끔하게 수정했습니다. 훨씬 간단하고 가독성이 좋습니다.
문제를 푸는 방법은 어렵지 않았으나, 100번 싸이클을 돌때 조금 실수해서 생각하면 바로 오답이 나올 수 있는 문제였습니다.
'알고리즘 > BAEKJOON' 카테고리의 다른 글
| 백준 16771번 Back and Forth (0) | 2019.04.01 |
|---|---|
| 백준 16770번 The Bucket List (0) | 2019.04.01 |
| 백준 16398번 행성연결 (0) | 2019.03.27 |
| 백준 7569번 토마토 (0) | 2019.03.27 |
| 백준 11049번 행렬 곱셈 순서 (0) | 2019.03.27 |