检查两个整数是否总和为三分之一

时间:2009-09-25 00:58:34

标签: java math

好的我正在做这个编程任务,需要一些帮助。

问题在于:

给定三个整数,一个b c,如果可以添加两个整数来获得第三个,则返回true。

twoAsOne(1, 2, 3) → true
twoAsOne(3, 1, 2) → true
twoAsOne(3, 2, 2) → false

这是我到目前为止所得到的:

public boolean twoAsOne(int a, int b, int c) {
  return a + b != c;
}

它一直说它不完全正确,我不知道我哪里出错了。

8 个答案:

答案 0 :(得分:11)

问题是询问是否可以添加任何两个来获取剩余的一个。只有当前两个添加到第三个​​时,您的代码才会进行测试。

因此,两个AsOne(3,1,2)应该返回true,因为3 = 1 + 2;但你只是检查3 + 1 = 2,这是假的。

答案 1 :(得分:7)

你只是检查一个的可能性,最重要的是,你正在检查错误,因为如果a + b =你将返回false = c(因为您正在使用!=运算符)。

我不打算为你做功课,但完整的可能性清单是:

n1 = n2 + n3
n2 = n1 + n3
n3 = n1 + n2

这应该是一件简单的事情:如果任何的结果为真,结果应为真。否则结果应该是假的。

或者,提供更明显的线索:如果 Else 它应该是 false

我不知道在没有为你编写代码的情况下我能做到的更明显: - )

更新:现在已经有足够的时间让家庭作业没有用了,这是我的解决方案:

public boolean twoAsOne (int n1, int n2, int n3) {
    if (n1 == n2 + n3) return true;
    if (n2 == n1 + n3) return true;
    if (n3 == n1 + n2) return true;
    return false;
}

虽然最后两行可以替换为:

    return (n3 == n1 + n2);

我更喜欢(对我而言)更具可读性的版本。

答案 2 :(得分:2)

除了itowlson和Pax提供的答案之外,由于你正在处理整数,它们有可能会溢出,例如。

Integer.MAX_VALUE + 1 == Integer.MIN_VALUE

这在数学上是不正确的

您可能需要检查此类方案以使程序完整。

答案 3 :(得分:1)

您的代码仅考虑int a和int b的总和。该解决方案需要涵盖所有可能性,即“int a和int c之和”和“int b和int c之和”。请参阅下面提到的代码,希望它有所帮助!

            <head>
            <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/ui-lightness/jquery-ui.css" />
             </head>
             <body>
            <script type="text/javascript" src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
            <script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js"></script>
            <script src="js/jquery.bxslider.min.js"></script>

            <link href="css/jquery.bxslider.css" rel="stylesheet" />

            <script type="text/javascript">
            $(document).ready(function(){
            $('.bxslider').bxSlider();
            $('.datepicker').datepicker({
                    showOn: 'button',
                buttonImage: 'images/dp.png',
                buttonImageOnly: true,
                });
            });
            </script>   
            </body>

答案 4 :(得分:0)

老兄,我希望你现在得到答案......如果你还没有

public boolean twoAsOne(int a, int b, int c) {
    return ((a+b==c) || (a+c==b) || (b+c==a));
}

答案 5 :(得分:0)

我可能已经很晚了但我已经把它降到最低了。 twoAsOne

public boolean twoAsOne(integer a, integer b, integer c){
    return ((a+b) == c ? true : (a+c) == b ? true : (b+c == a)? true : false);
}

答案 6 :(得分:0)

package get_third;

public class Get_third {
    int a , b ,c ;
    boolean third_value(int a , int b , int c){

    if(a+b==c||b+c==a||c+a==b)
    {
        return true;
    }
    else
        return false ;
    }

    public static void main(String[] args) {
        Get_third obj =new Get_third();
        System.out.println(obj.third_value(1, 2, 3));
        System.out.println(obj.third_value(3, 1, 2));
        System.out.println(obj.third_value(3, 2, 2));      
    }
}

答案 7 :(得分:0)

//开始

public boolean twoAsOne(int a, int b, int c) {

    if (a + b == c) {
        return true;
    }
    else if (a + c == b) {
        return true;
    }
    else if (b + c == a) {
        return true;
    }
    else {
        return false;
    }
}

//结束