2019 GDUT Rating Contest I : Problem H. Mixing Milk
題面:
H. Mixing Milk
Input ?le: standard input Output ?le: standard output Time limit: 1 second Memory limit: 256 megabytes Farming is competitive business – particularly milk production. Farmer John ?gures 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 di?erent taste, and he plans to mix these together to get the perfect blend of ?avors.To mix the three di?erent milks, he takes three buckets containing milk from the three cows. The buckets may have di?erent 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 ?nishes all 100 pours. Input The ?rst line of the input ?le contains two space-separated integers: the capacity c1 of the ?rst bucket, and the amount of milk m1 in the ?rst 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. Output Please print three lines of output,giving the ?nal amount of milk in each bucket, after 100 pouroperations. Example Input 10 3 11 4 12 5 Output 0 10 2 Note In this example, the milk in each bucket is as follows during the sequence of pours: Initial State: 3 4 5- Pour 1->2: 0 7 5
- Pour 2->3: 0 0 12
- Pour 3->1: 10 0 2
- Pour 1->2: 0 10 2
- Pour 2->3: 0 0 12
題目描述:
有三個桶,每個桶都有不同量的牛奶。第一次:把第1個桶的牛奶倒進第2個桶,直到第2個桶倒滿或者第1個桶的牛奶倒完。第二次:把第2個桶的牛奶倒進第3個桶,直到第3個桶倒滿或者第2個桶的牛奶倒完。第三次:把第3個桶的牛奶倒進第1個桶,直到第1個桶倒滿或者第3個桶的牛奶倒完。第四次:重復第一次的操作。第五次:重復第二次的操作......,進行了100次倒牛奶的操作,問:現在三個桶里面有多少牛奶?題目分析:
這道題直接模擬:我們分析一下”倒“牛奶的操作: 把左邊桶的牛奶倒進右邊的桶,有兩種情況: 1.左邊的牛奶全部倒進右邊: 2.左邊的牛奶還有剩余: ? 對于第一種情況,先把左邊牛奶的量加到右邊,然后才把左邊的牛奶的量清空(直接設為0)。這里容易錯的地方就是有的人會把這兩個的先后順序搞反,導致自己看起來好像明明邏輯對了,但結果就是不對。原因:如果先把左邊牛奶的量清空,我們就不知道要從左邊倒多少牛奶到右邊,錯誤代碼導致的示意圖: 對于第二種情況,先把左邊剩下多少牛奶計算出來,然后把右邊的牛奶加滿(直接設為容量)。這里容易錯的地方道理和剛剛差不多: ? 最后,我們把這個過程弄一個函數,然后按照題目意思直接調用這個倒牛奶的函數就行了。(雖然是道水題,但也不能忽視一些小錯誤o(≧口≦)o,寫題解時發現自己當時的ac代碼(無函數版)真丑┭┮﹏┭┮) AC代碼:1 #include <cstdio> 2 #include <cstring> 3 #include <iostream> 4 using namespace std; 5 int capa[5], leave[5]; 6 7 void pour(int a, int b){ 8 if(leave[a]+leave[b] <= capa[b]){ //第一種情況 9 leave[b] += leave[a]; 10 leave[a] = 0; 11 } 12 else{ //第二種情況 13 leave[a] -= capa[b]-leave[b]; 14 leave[b] = capa[b]; 15 } 16 } 17 18 int main(){ 19 for(int i = 1; i <= 3; i++){ 20 cin >> capa[i] >> leave[i]; 21 } 22 23 for(int i = 0; i < 33; i++){ 24 pour(1, 2); //桶1倒進桶2 25 pour(2, 3); //桶2倒進桶3 26 pour(3, 1); //桶3倒進桶1 27 } 28 29 pour(1, 2); //最后別忘這個 30 31 for(int i = 1; i <= 3; i++){ 32 cout << leave[i] << endl; 33 } 34 return 0; 35 }
?
?
轉載于:https://www.cnblogs.com/happy-MEdge/p/10530730.html
總結
以上是生活随笔為你收集整理的2019 GDUT Rating Contest I : Problem H. Mixing Milk的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 给头发做定型一般都多少钱?能坚持多久?做
- 下一篇: 求如果感到幸福你就拍拍手歌词。