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 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 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;}








0 0
原创粉丝点击