题面:

H. Mixing Milk

Input file: standard input
Output file: standard output
Time limit: 1 second
Memory limit: 256 megabytes
 
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.
 
Input
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.
 
Output
Please print three lines of output,giving the final 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
  1. Pour 1->2: 0 7 5
  2. Pour 2->3: 0 0 12
  3. Pour 3->1: 10 0 2
  4. Pour 1->2: 0 10 2
  5. Pour 2->3: 0 0 12
(The last three states then repeat in a cycle ...)
 

题目描述:

有三个桶,每个桶都有不同量的牛奶。第一次:把第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 }
 
 

2019 GDUT Rating Contest I : Problem H. Mixing Milk的更多相关文章

  1. 2019 GDUT Rating Contest II : Problem F. Teleportation

    题面: Problem F. Teleportation Input file: standard input Output file: standard output Time limit: 15 se ...

  2. 2019 GDUT Rating Contest III : Problem D. Lemonade Line

    题面: D. Lemonade Line Input file: standard input Output file: standard output Time limit: 1 second Memo ...

  3. 2019 GDUT Rating Contest I : Problem A. The Bucket List

    题面: A. The Bucket List Input file: standard input Output file: standard output Time limit: 1 second Me ...

  4. 2019 GDUT Rating Contest I : Problem G. Back and Forth

    题面: G. Back and Forth Input file: standard input Output file: standard output Time limit: 1 second Mem ...

  5. 2019 GDUT Rating Contest III : Problem E. Family Tree

    题面: E. Family Tree Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  6. 2019 GDUT Rating Contest III : Problem C. Team Tic Tac Toe

    题面: C. Team Tic Tac Toe Input file: standard input Output file: standard output Time limit: 1 second M ...

  7. 2019 GDUT Rating Contest III : Problem A. Out of Sorts

    题面: 传送门 A. Out of Sorts Input file: standard input Output file: standard output Time limit: 1 second M ...

  8. 2019 GDUT Rating Contest II : Problem G. Snow Boots

    题面: G. Snow Boots Input file: standard input Output file: standard output Time limit: 1 second Memory ...

  9. 2019 GDUT Rating Contest II : Problem C. Rest Stops

    题面: C. Rest Stops Input file: standard input Output file: standard output Time limit: 1 second Memory ...

随机推荐

  1. Redis面试常见问题(一)

    一.redis 简介简单来说 redis 就是一个数据库,不过与传统数据库不同的是 redis 的数据是存在内存中的,所以读写速度非常快,因此 redis 被广泛应用于缓存方向.另外,redis 也经 ...

  2. 如何加入VNT Hubble主网

    环境:Ubuntu20.04 (但以下方法应该只要不是过于老旧的Ubuntu,都行得通) 从源码安装go-vnt 安装Go编译器(版本大于1.9)和C编译器 安装C编译器GCC[1] sudo apt ...

  3. 初学算法之最基础的stl队列

    简记为先进先出(first in first out) 它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作. 实用: #include <queue>//头 ...

  4. python3基本数据类型补充

    列表 list 有序,可嵌套,可重复,元素可修改 方括号 占用空间小但时间消耗比较大 mylist=["kimi",1,1,1,["amy",18]] V=my ...

  5. Linux POSIX共享内存方法&ipcs &struct shmid_ds

    内容是主进程创建子进程计算斐波那契数列. 其中计算到第几项是有主进程命令行输入. 共享内存段,并且查看了一些信息. 参考操作系统概念第七版 3.10,3.11 关于LINUX C库函数 中的 fpri ...

  6. 关于HashMap遍历,为什么要用entry

    Map.entrySet() 这个方法返回的是一个Set<Map.Entry<K,V>>,Map.Entry 是Map中的一个接口,他的用途是表示一个映射项(里面有Key和Va ...

  7. React Hooks: useLayoutEffect All In One

    React Hooks: useLayoutEffect All In One useLayoutEffect https://reactjs.org/docs/hooks-reference.htm ...

  8. js console 性能测试 & don't-use-array-foreach-use-for-instead

    don't-use-array-foreach-use-for-instead slower https://coderwall.com/p/kvzbpa/don-t-use-array-foreac ...

  9. css var all in one & html & root & :root

    css var all in one number :root{ --num: 0; } html{ --num: 0; } let html = document.querySelector(`ht ...

  10. modal 弹框遮罩层,滚动穿透bug 解决方案

    modal 弹框遮罩层,滚动穿透bug 解决方案 parent component 动态设置 lock css const computedClassName = classNames( 'activ ...