Change the ball
2015-08-02 18:40
405 查看
Change the ball
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 2
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?
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 3
1 2 2
Sample Output
):
2
分析:这个题意是给出三种颜色小球的个数,而且任意两种颜色小球碰撞回变成第三种颜色!
求出3种颜色小球变成一种颜色的最少步数!
分析一下,最直接的就是有两种小球的个数相等,不相等的要想办法转化!每次转化一次两种小球个数各减一,另一种会加二,这中间就差了3!也就是说如果两种小球的个数是3的倍数,就可以转化为两种小球相等,就可以转化为一种颜色!
设三球数量各不相等要转换成a[2];应先使a[0]与a[1]的数量相等,设调换k次,调换后三球数量分别为a[0]+2k,a[1]-k,a[2]-k;此时满足a[0]+2k==a[1]-k;所以a[1]-a[0]==3k,总共需要转换的次数为k+a[1]-k==a[1]次也可算做k+a[0]+2k==k+a[1]-3k+2k==a[1]次
要注意,第一个if语句与第二个else if语句绝对不能调换,若调换就会输出错误答案,如调换后输入2 5 8输出的是8
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 26 Accepted Submission(s) : 2
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?
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 3
1 2 2
Sample Output
):
2
分析:这个题意是给出三种颜色小球的个数,而且任意两种颜色小球碰撞回变成第三种颜色!
求出3种颜色小球变成一种颜色的最少步数!
分析一下,最直接的就是有两种小球的个数相等,不相等的要想办法转化!每次转化一次两种小球个数各减一,另一种会加二,这中间就差了3!也就是说如果两种小球的个数是3的倍数,就可以转化为两种小球相等,就可以转化为一种颜色!
设三球数量各不相等要转换成a[2];应先使a[0]与a[1]的数量相等,设调换k次,调换后三球数量分别为a[0]+2k,a[1]-k,a[2]-k;此时满足a[0]+2k==a[1]-k;所以a[1]-a[0]==3k,总共需要转换的次数为k+a[1]-k==a[1]次也可算做k+a[0]+2k==k+a[1]-3k+2k==a[1]次
要注意,第一个if语句与第二个else if语句绝对不能调换,若调换就会输出错误答案,如调换后输入2 5 8输出的是8
#include<stdio.h> #include<algorithm> using namespace std; int main() { int a[3]; while(scanf("%d%d%d",&a[0],&a[1],&a[2])!=EOF) { sort(a,a+3); if(a[1]==a[0]||(a[1]-a[0])%3==0) printf("%d\n",a[1]); else if(a[2]==a[1]||(a[2]-a[1])%3==0||(a[2]-a[0])%3==0) printf("%d\n",a[2]); else printf("):\n"); } return 0; }
相关文章推荐
- 自定义普通文本TableView header footer 视图
- csdn 回复问题
- 来 不 及 认 真 的 年 轻
- 按工资降序排列,若工资相同,则按照雇佣日期尚需排列,获取6~10 条记录
- uva 10325 容斥原理
- N!Again
- string的初始化
- [设计模式]<<设计模式之禅>>抽象工厂模式
- 好纠结啊
- GCC警告
- Java_Web三大框架之Hibernate+jsp+HQL分页查询
- jboss7 jndi使用
- java错误篇二
- android的样式(style)与主题(theme)
- DNA Sorting
- RPC远程过程调用协议
- QT安装以及环境配置
- Android源码分析-全面理解Context
- nyoj106背包问题为模板解出杭电oj2187悼念512汶川大地震遇难同胞——老人是真饿了
- innodb之change buffer主动merge