zoukankan      html  css  js  c++  java
  • HDU 2277 Change the ball

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2277

    Change the ball

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 694    Accepted Submission(s): 272


    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
    题目大意:有三种颜色的球(黄蓝黄),数量各不相同,现在给定每种球的个数,b并且有一种规则(拿起任意两种不同颜色的球他们的颜色会立马变成第三种颜色),问能否经过变换后所有的球都变成同一种颜色、需要的最少次数。
    解题思路:能够达到目的的有两种情况:

    ①有两个球的数量相等(或者是三个都相等)

    ②有一种球是另一种球数量的三倍(每改变一次,有一个球会增加2个,另两个会各减少1个,中间差了3,也就是说,只有是三的倍数的情况,三个球才能实现完全转化),这种条件就需要分情况了,因为可能是都转化为球最多的那个,也有可能是都转化成球数不是最多的那个(最少次数就为球最多的球数(可以等价为这种球一直在向别的转化))

    AC代码:

     1 #include <cstdio>  
     2 #include <algorithm>  
     3 using namespace std;  
     4 int main()  
     5 {  
     6     int a[4];  
     7     while (~scanf ("%d %d %d",&a[1],&a[2],&a[3]))  
     8     {  
     9         sort (a+1,a+1+3);  
    10         if ( a[2] == a[1] || (a[2] - a[1]) % 3 ==0 )        //先看是否能把最少的转化成多的   
    11             printf ("%d
    ",a[2]);       //如果可以的话,实际上是 a[2] 一直向 a[1]和 a[3]转化   
    12         else if ( a[2] == a[3] || (a[3] - a[2]) % 3 == 0 )   
    13             printf ("%d
    ",a[3]);       //结合上一种,这一种也可以理解   
    14         else if ( a[1] == a[3] || (a[3] - a[1]) % 3 == 0 )  
    15             printf ("%d
    ",a[3]);       //实际上,这种情况完全可以和上一种合并,但是为了程序的清楚就分开了  
    16         //else if ( a[2] == a[3] || (a[3] - a[2]) % 3 == 0 || a[1] == a[3] || (a[3] - a[1]) % 3 == 0 )  
    17         //  printf ("%d
    ",a[3]);  
    18         //最后两个 else if 可以合并成这样  
    19         else  
    20             printf ("):
    ");  
    21     }  
    22     return 0;  
    23 }  
     
  • 相关阅读:
    团队冲刺第五天
    单词统计包括其续
    团队冲刺第四天
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    软件工程第九周总结
    软件工程第八周总结
    《大道至简》阅读笔记二
    《大道至简》阅读笔记一
  • 原文地址:https://www.cnblogs.com/yoke/p/5936665.html
Copyright © 2011-2022 走看看