Change the ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 439 Accepted Submission(s): 42
Total Submission(s): 439 Accepted Submission(s): 42
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?
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 “):”.
SampleInput
1 2 3 1 2 2
SampleOutput
): 2
思路:
一球-1,一球+2,要想相同肯定要球数差值为3的倍数,而且根据数学推导,消去后刚好就是除数大小,
实际问题就转发求差值为3的倍数的两数相除,其中除数大小的最小值。
当时多想了一点,说不清楚,附上自己错误代码(领了三发WA,是我的锅):
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int a[3];
while(cin>>a[0]>>a[1]>>a[2]&&a[0]&&a[1]&&a[2])
{
sort(a,a+3);
if((a[1]-a[0])%3==0) cout<<a[1]<<endl;
else if((a[2]-a[0])%3==0&&((a[2]-a[0])/3<=a[1])) cout<<a[2]<<endl;
else if((a[2]-a[1])%3==0&&((a[2]-a[1])/3<=a[0])) cout<<a[2]<<endl;
else cout<<"):"<<endl;
}
return 0;
}
一直找不到WA点。
后面所幸盛巨巨找了0,1,9这组数据,
WA点出来了。
原因是:
(0,1,9)--->(2,0,8)--->(1,2,7)--->……
根据这个上述原理,还可以继续这样下去。
所以正确代码应该如下:
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
int main()
{
int a[3];
while(cin>>a[0]>>a[1]>>a[2]&&a[0]&&a[1]&&a[2])
{
sort(a,a+3);
if((a[1]-a[0])%3==0) cout<<a[1]<<endl;
else if((a[2]-a[0])%3==0) cout<<a[2]<<endl;
else if((a[2]-a[1])%3==0) cout<<a[2]<<endl;
else cout<<"):"<<endl;
}
return 0;
}
火候不够还需努力。