您的位置:首页 > 其它

2277 Change the ball

2015-07-26 01:34 344 查看

Change the ball

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 559    Accepted Submission(s): 196

[align=left]Problem Description[/align]
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?

 

[align=left]Input[/align]
For each line, there are three intergers Y, B, R(1<=Y,B,R<=1000),indicate the number refered above.
 

[align=left]Output[/align]
For each case, tell Garfield the minimal steps to complete the assignment. If not, output the symbol “):”.
 

[align=left]Sample Input[/align]

1 2 3
1 2 2

 

[align=left]Sample Output[/align]

):
2

题意:

有个人有三种颜色的球若干个,每两个不同颜色的球相遇,就可以转化为第三种颜色,给出每个球的数目,求是否可以最终转化为一种颜色.......

这个题是个规律题.............

然后可以推导出来,只要任意两个球相差的数可以被三整除,那么这个就可以成功转化为一种颜色的情况........

具体什么原因,就好像牵扯到排列组合什么的知识了...特别复杂............

题解:

比如输入 a,b,c,那么只要想办法,能转化为有两个相等的就一定能达到要求,再继续想,什么情况能转化为有两个相同呢,那就需要其中一个球减少,一个球增多(来回碰实现),注意,减少的是一个一个减少,增加是两个两个增加,所以,肯定要满足有两个数的差为 3 的倍数!!!

这个就是推出的规律了.....不信可以验证一下看看....

输出的结果:

假设出现的是满足要求的一种一般情况((a-b)%3==0&&b>a),首先把a,b数量转成一样的即(b-a)/3*2+a个,需要(b-a)/3次,在把a,b转化为c需要(b-a)/3*2+a次,总次数为(b-a)/3+((b-a)/3)*2+a=b次....就这样,不要考虑转化的时候会不会有一个不够,不够的完全可以别的转化来,也就是这样就是一般情况了

规律是求出来了,但是这样就能ac了吗??

下面一组代码:

#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
int main()
{
int a,b,c;
while(~scanf("%d%d%d",&a,&b,&c))
{
if((a-b)%3==0)
{
printf("%d\n",max(a,b));
}
else if((b-c)%3==0)
{
printf("%d\n",max(b,c));
}
else if((a-c)%3==0)
{
printf("%d\n",max(a,c));
}
else
{
printf("):\n");
}
}
return 0;
}

结果是答案错误!!!!!!!

后来想想为啥呢,才发现问题,不信自己跑跑 10 4 1 和 1 10 4 试试看......

看来有多种方案的时候,还需要找到最优的方案,只能先排序了......

#include<stdio.h>
#include<algorithm>
using namespace std;
int main()
{
int x[3];
while(~scanf("%d%d%d",&x[0],&x[1],&x[2]))
{
sort(x,x+3);
if((x[1]-x[0])%3==0)
{
printf("%d\n",x[1]);
}
else if((x[2]-x[0])%3==0)
{
printf("%d\n",x[2]);
}
else if((x[2]-x[1])%3==0)
{
printf("%d\n",x[2]);
}
else
{
printf("):\n");
}
}
return 0;
}
这样就避免了那样的错误,ac了,以后考虑问题,一定要考虑周到,结果光顾着找规律,也没注意到这样的特殊情况,一定要细心再细心! 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: