Change the ball
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 515 Accepted Submission(s): 187
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 “):”.
Sample Input
1 2 3 1 2 2
Sample Output
): 2
Source
Recommend
#include<stdio.h>
#include<algorithm>
using namespace std;
int main(){
int a[3];
while(~scanf("%d%d%d",&a[0],&a[1],&a[2])){
sort(a,a+3);
if(a[0]==a[1]||(a[1]-a[0])%3==0)
printf("%d\n",a[1]);
else if(a[1]==a[2]||(a[2]-a[1])%3==0||(a[2]-a[0])%3==0)
printf("%d\n",a[2]);
else printf("):\n");
}
return 0;
}
策略:这道题假设有相同的那么显然就是相同的数目,如果没有相同的,如果能转化同一个颜色,那么必有(s - n)%3 == 0,即两种颜色的球的个数差,是3的倍数(仔细想一下),所以 我们排一下序,依次判断就可以了
!!!!!!!不太明白,百度找的 ,实在没看出来:(S-N)%3==0 这规律是怎么找出来的,!!!!!!