【HDU2277】Change the ball(类似贪心策略,数学计算)

Categories: 数据结构和算法
Comments: No Comments
Published on: 2011 年 05 月 17 日

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?
题意:有三种球,每两种球各一个可以变换成第三种球两个。这种规则不能反过来使用,求最少的步数能够把三种球换成1种球。
题解:首先考虑三种球(a、b、c)的各种情况:
第一种情况:a==b!=c(即有两种球数量相等)此时res=a;
第二种情况:a==b==c(即三种球数量都相等) 此时res=a;
第三种情况:a #include <iostream> #include <algorithm> using namespace std; int main() { int a[3]; while (cin>>a[0]>>a[1]>>a[2]) { sort(a,a+3); int res=a[2]; if(a[0]==a[1]) { res=a[0]; }else if(a[1]==a[2]) { res=a[1]; }else if((a[1]-a[0])%3 == 0) { res=a[1]; }else if((a[2]-a[1])%3==0) { res=min(res,a[2]); }else if((a[2]-a[0])%3==0) { res=min(res,a[2]); } else {cout<<"):"<<endl; continue;} cout<<res<<endl; } }

我猜你可能也喜欢:

No Comments - Leave a comment

Leave a comment

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>


Welcome , today is 星期六, 2017 年 10 月 21 日