Change the ball(找规律)

Change the ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 552    Accepted Submission(s): 193


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
题解:如果可以变为一种颜色,中间必有两个数相等,假设气球数目刚开始为x,y,z;当碰撞n次时则有,x-n,y+2n,z-n;
若想成功,则有x-n=y+2n或y+2n=z-n;则有x-y=3n或x-z=3n;此时需要再碰撞x-n次则成功,由于刚开始已经碰了n次,则总次数为x-n+n=x也就是较大的数;
则有任意两个数相减出现3的倍数时碰撞成功,否则失败;另外还要找到最小次数,所以sort排序;
代码:
Change the ball(找规律)
 1 #include<stdio.h>

 2 #include<algorithm>

 3 using namespace std;

 4 #define MAX(x,y) x>y?x:y

 5 #define MIN(x,y) x<y?x:y

 6 int judge(int x,int y){

 7     int a,b;

 8     a=MAX(x,y);

 9     b=MIN(x,y);

10     if((a-b)%3==0)return a;

11     else return 0;

12 }

13 int main(){

14     int Y,B,R,temp[3],flot;

15     while(~scanf("%d%d%d",&Y,&B,&R)){flot=1;

16     temp[0]=judge(Y,B),temp[1]=judge(Y,R),temp[2]=judge(B,R);

17         sort(temp,temp+3);

18         for(int i=0;i<3;i++){

19             if(temp[i]){flot=0;

20                 printf("%d\n",temp[i]);break;

21             }

22         }

23         if(flot)printf("):\n");

24     }

25     return 0;

26 }
View Code

 

你可能感兴趣的:(change)