【杭电oj】2277 - Change the ball(找规律)
来源:互联网 发布:线割编程招聘 编辑:程序博客网 时间:2024/04/19 11:40
Change the ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 621 Accepted Submission(s): 224
Problem Description
Garfield has three piles of balls, each pile has unique color of following: yellow, blue, and red. Now we also know Garfield has Y yellow balls, B blue balls, and R red balls. But Garfield just wants to change all the balls to one color. When he puts two balls of different color togather, the balls then change their colors automatically into the rest color. For instance, when Garfield puts a red one and a yellow one togather, the two balls immediately owns blue color, the same to other situations. But the rule doesn’t work when the two balls have the same color.
Garfield is not able to estimate the minimal steps to achieve the aim. Can you tell him?
Garfield is not able to estimate the minimal steps to achieve the aim. Can you tell him?
Input
For each line, there are three intergers Y, B, R(1<=Y,B,R<=1000),indicate the number refered above.
Output
For each case, tell Garfield the minimal steps to complete the assignment. If not, output the symbol “):”.
Sample Input
1 2 31 2 2
Sample Output
):2
Source
HDU 8th Programming Contest Site(1)
这种题还是需要观察找规律的,如果可以完成任务有几种情况:
①有两个球的数量相等(或者是三个都相等)
②有一种球是另一种球数量的三倍(每改变一次,有一个球会增加2个,另两个会各减少1个,中间差了3,也就是说,只有是三的倍数的情况,三个球才能实现完全转化),这种条件就需要分情况了,因为可能是都转化为球最多的那个,也有可能是都转化成球数不是最多的那个(最少次数就为球最多的球数(可以等价为这种球一直在向别的转化))
结合一下代码,比较好理解:
代码如下:
#include <cstdio>#include <algorithm>using namespace std;int main(){int a[4];while (~scanf ("%d %d %d",&a[1],&a[2],&a[3])){sort (a+1,a+1+3);if ( a[2] == a[1] || (a[2] - a[1]) % 3 ==0 )//先看是否能把最少的转化成多的 printf ("%d\n",a[2]);//如果可以的话,实际上是 a[2] 一直向 a[1]和 a[3]转化 else if ( a[2] == a[3] || (a[3] - a[2]) % 3 == 0 ) printf ("%d\n",a[3]);//结合上一种,这一种也可以理解 else if ( a[1] == a[3] || (a[3] - a[1]) % 3 == 0 )printf ("%d\n",a[3]);//实际上,这种情况完全可以和上一种合并,但是为了程序的清楚就分开了//else if ( a[2] == a[3] || (a[3] - a[2]) % 3 == 0 || a[1] == a[3] || (a[3] - a[1]) % 3 == 0 )//printf ("%d\n",a[3]);//最后两个 else if 可以合并成这样elseprintf ("):\n");}return 0;}
0 0
- 【杭电oj】2277 - Change the ball(找规律)
- hdoj 2277 Change the ball 【找规律】
- hdu 2277 Change the ball(找规律)
- hdoj 2277 Change the ball 【找规律】
- hdoj 2277 Change the ball 【找规律】
- HDU 2277 Change the ball(找规律)
- 杭电2277 Change the ball
- HDOJ 题目2277 Change the ball(水题,规律)
- hdoj-2277-Change the ball【规律】
- hdoj 2277 Change the ball【规律题】
- 【杭电oj】2524 - 矩形A + B(找规律)
- 【杭电oj】1005 - Number Sequence(找规律)
- 【杭电oj】1556 - Color the ball(区间更新)
- 【杭电-oj】-1556-Color the ball(区间更新)
- 2277 Change the ball
- 杭电 OJ Color the ball 树状数组
- hdu-2277-Change the ball
- HDU-2277 Change the ball
- Facebook Phone Interview -- Move Zeros to Right (Easy)
- 嵌入式编程基础知识学习(1)
- Spring源码之bean的加载(四)获取单例
- 并查集
- Spring源码之bean的加载(五)准备创建bean
- 【杭电oj】2277 - Change the ball(找规律)
- Using multiple LLVM versions on Ubuntu
- 【BZOJ 4443】 [Scoi2015]小凸玩矩阵|二分|最大匹配|匈牙利
- Java命名规范及给自己写的方法添加注解
- 【BZOJ 4448】 [Scoi2015]情报传递|树链剖分|树套树
- spring初步和struts2整合
- [J2EE]java操作json
- 用数学方法解决工程问题系列(三) 判断任意给定一点是否在多边形内
- [bzoj2440][中山市选2011]完全平方数