HDU:2277 Change the ball(水)(数学)
来源:互联网 发布:知乎炸鱼为业 编辑:程序博客网 时间:2024/05/17 17:58
Change the ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 656 Accepted Submission(s): 240
Problem DescriptionGarfield 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?
InputFor each line, there are three intergers Y, B, R(1<=Y,B,R<=1000),indicate the number refered above.
OutputFor each case, tell Garfield the minimal steps to complete the assignment. If not, output the symbol “):”.
Sample Input1 2 31 2 2
Sample Output):2题目大意:加菲猫有三种颜色的球a,b,c。定义一种规则,a球和b球碰撞能生成2个c球,a球和c球碰撞能生成2个b球,b球和c球碰撞能生成2个a球,反之则不成立。求将这三种球变成一种球最小需要多少步骤。分析:(1)若a,b,c中有两两相等的数目的球,即a==b或a==c或b==c,则最短步骤为将这两球直接转换成第三种球,所以步骤数为这两种数目相等球的数目。(2)三个球数目不相等,则需先将两种球的数目先变相等,接下来就为步骤一了。用a代表a球的数目,b代表b球的数目,c代表c球的数目,如将a、b数目变相等,假设b>a,则需用b、c碰撞变为a,每失去一个b和c就得到两个a,所以当a、b两球相等时,设b失去x个,则有式子b-x=a+2x,即x=(b-a)/3,b失去x个球之后剩余个数为b-x,此时a的个数也为b-x,所以a、b再转化为c的时候需要b-x步,加上之前将a、b数目变相等的步骤x,所以最终步骤为b-x+x=b。(2)情况的总结:①要想将两种球数目变相等,则两球数目初始数目之差必须为3的整数倍;②符合条件①之后最终答案步骤数为两球中数目较大的那种球的数目。#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[0]==a[1]||a[1]==a[2]){printf("%d\n",a[1]);continue;}if((a[1]-a[0])%3==0){int sum=0;sum=sum+(a[1]-a[0])/3;a[1]=a[1]-(a[1]-a[0])/3;sum=sum+a[1];printf("%d\n",sum);continue;}if((a[2]-a[1])%3==0){int sum=0;sum=sum+(a[2]-a[1])/3;a[2]=a[2]-(a[2]-a[1])/3;sum=sum+a[2];printf("%d\n",sum);continue;}if((a[2]-a[0])%3==0){int sum=0;sum=sum+(a[2]-a[0])/3;a[2]=a[2]-(a[2]-a[0])/3;sum=sum+a[2];printf("%d\n",sum);continue;}printf("):\n");}return 0;}
Garfield is not able to estimate the minimal steps to achieve the aim. Can you tell him?
1 2 31 2 2
):2题目大意:加菲猫有三种颜色的球a,b,c。定义一种规则,a球和b球碰撞能生成2个c球,a球和c球碰撞能生成2个b球,b球和c球碰撞能生成2个a球,反之则不成立。求将这三种球变成一种球最小需要多少步骤。分析:(1)若a,b,c中有两两相等的数目的球,即a==b或a==c或b==c,则最短步骤为将这两球直接转换成第三种球,所以步骤数为这两种数目相等球的数目。(2)三个球数目不相等,则需先将两种球的数目先变相等,接下来就为步骤一了。用a代表a球的数目,b代表b球的数目,c代表c球的数目,如将a、b数目变相等,假设b>a,则需用b、c碰撞变为a,每失去一个b和c就得到两个a,所以当a、b两球相等时,设b失去x个,则有式子b-x=a+2x,即x=(b-a)/3,b失去x个球之后剩余个数为b-x,此时a的个数也为b-x,所以a、b再转化为c的时候需要b-x步,加上之前将a、b数目变相等的步骤x,所以最终步骤为b-x+x=b。(2)情况的总结:①要想将两种球数目变相等,则两球数目初始数目之差必须为3的整数倍;②符合条件①之后最终答案步骤数为两球中数目较大的那种球的数目。#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[0]==a[1]||a[1]==a[2]){printf("%d\n",a[1]);continue;}if((a[1]-a[0])%3==0){int sum=0;sum=sum+(a[1]-a[0])/3;a[1]=a[1]-(a[1]-a[0])/3;sum=sum+a[1];printf("%d\n",sum);continue;}if((a[2]-a[1])%3==0){int sum=0;sum=sum+(a[2]-a[1])/3;a[2]=a[2]-(a[2]-a[1])/3;sum=sum+a[2];printf("%d\n",sum);continue;}if((a[2]-a[0])%3==0){int sum=0;sum=sum+(a[2]-a[0])/3;a[2]=a[2]-(a[2]-a[0])/3;sum=sum+a[2];printf("%d\n",sum);continue;}printf("):\n");}return 0;}
0 0
- HDU:2277 Change the ball(水)(数学)
- HDU 2277 Change the ball(找规律)
- hdu-2277-Change the ball
- HDU-2277 Change the ball
- HDU-2277 Change the ball
- hdu 2277 Change the ball(找规律)
- 2277 Change the ball
- HDOJ 题目2277 Change the ball(水题,规律)
- 【杭电oj】2277 - Change the ball(找规律)
- nyoj Change the ball 313 (技巧)
- hdoj 2277 Change the ball
- nyoj Change the ball 313 (技巧&思想)
- 杭电2277 Change the ball
- hdoj 2277 Change the ball 【找规律】
- hdoj 2277 Change the ball 【找规律】
- hdoj-2277-Change the ball【规律】
- hdoj 2277 Change the ball 【找规律】
- hdoj 2277 Change the ball【规律题】
- 关于《编码:隐匿在计算机软硬件背后的语言》
- 多线程:延时执行
- 很腻害的视频,回味百遍
- 《写在伤心的时候》
- 周鸿祎:很多程序员聪明,但我一看就知道他不会成功
- HDU:2277 Change the ball(水)(数学)
- css样式
- 标准类的写法
- 多线程:调度组
- MVC 列表分页 Partial内容页
- prism model check 的环境安装设置
- 用TCL(工具命令语言)管理Tornado (for VxWorks) 可启动工程
- JAVA大数据运算系列之大整数相乘
- 设计模式之工厂三姐妹解读(一)