您的位置:首页 > 其它

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

#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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: