求abc是a2 + b2 + c2 = a + b + c [英] Find abc which is a2+b2+c2=a+b+c

查看:214
本文介绍了求abc是a2 + b2 + c2 = a + b + c的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,



所以我必须制作一个程序,找到abc,三位数字,对于a2 + b2 + c2 = a + b + c (不知道在我的第一篇文章中有什么不清楚...)我为此制作了一个代码,但我不知道它是否好,因为它只给我4个数字,范围在100-1000.Can any1检查它请参阅它是正确的。只有4个答案??或者我的smtg错了,这就是为什么我只给了我4.Thx提前





  static   int  a; 
static int b;
static int c;
static int abc;
static int sum;
static int sum2;




public static void main( String [] args){

for (abc = 100 ; abc& lt; 1000 ; abc ++ ){

a = abc / 100;
b = abc%100/10;
c = abc%10;
sum = a + b + c;

sum2 =( int )(Math.pow(a, 2 ) + Math.pow(b, 2 )+ Math.pow(c, 2 ));
if (sum == sum2){
System。 out .println( + abc);

}



}

解决方案

你的答案是正确的,这是有道理的,因为如果任何数字大于1,则条件必须为假:



  public   static   void  ABC()
{
int abc = 0 ,a2b2c2 = 0 ,num = 0 ;
for int a = 0 ; a< 10; a ++)
{
for int b = 0 ; b< 10; b ++)
{
for int c = 0 ; c< 10; c ++)
{
abc = a + b + C;
a2b2c2 = a * a + b * b + c * c;
num = 100 * a + 10 * b + c;
if (abc == a2b2c2&& num> = 100
系统。 out .println(num);
}
}
}
}


我没有很多Java经验,所以我在C#中测试了你的代码(有很小的变化):

  for  int  abc =  100 ; abc< 1000; abc ++)
{
int a = abc / 100;
int b = abc%100/10;
int c = abc%10;
int sum = a + b + c;
int sum2 =( int )(System.Math.Pow(a, 2 )+ System.Math.Pow(b, 2 )+ System.Math.Pow(c, 2 ));
if (sum == sum2)
{
Console.WriteLine( abc = {0} a = {1} b = {2} c = {3} sum = {4} sum2 = {5},abc ,a,b,c,sum,sum2);
}
}







并使用Linq查询:

  var  qry = Enumerable.Range( 100  900 )。 
选择(z = > new {a =( int )z / 100,b =( int )z%100/10,c =( INT )z的%10})。
选择(n = > new {a = na,b = nb,c = nc,sum = n.a + n.b + nc,sum2 =( int )(Math.Pow(na, 2 )+ Math.Pow(nb, 2 )+ Math.Pow(nc, 2 ))})。
其中(x => x.sum == x.sum2)。
选择(f => f);





结果相同:

< pre lang =text> abc sum sum2
1 0 0 1 1
1 0 1 2 2
1 1 0 2 2
1 1 1 3 3


Hello,

So i have to make a program which finds abc, three digit number which is true to a2+b2+c2=a+b+c.(Dont know what was unclear on this in my first post...)I made a code for this but i dont know if its good because it gives me only 4 numbers in range of 100-1000.Can any1 check it pls if its correct.There is only 4 answers??Or i made smtg wrong and thats why i gives me only 4.Thx in advance


static int a;
static int b;
static int c;
static int abc;
static int sum;
static int sum2;




public static void main(String [] args){

    for(abc=100;abc&lt;1000;abc++){

        a=abc/100;
        b=abc%100/10;
        c=abc%10;
        sum=a+b+c;

        sum2 = (int) (Math.pow(a, 2)+ Math.pow(b, 2)+Math.pow(c, 2));
    if(sum==sum2){
        System.out.println(" "+abc);

    }



    }

解决方案

Your answer is correct, this makes sense since if any digit was greater than 1 the condition must be false:

public static void ABC()
    {
        int abc=0,a2b2c2=0,num=0;
        for(int a=0; a<10; a++)
        {
            for(int b=0; b<10; b++)
            {
                for(int c=0; c<10; c++)
                {
                    abc=a+b+c;
                    a2b2c2=a*a+b*b+c*c;
                    num=100*a+10*b+c;
                    if(abc == a2b2c2 && num>=100)
                        System.out.println(num);
                }
            }
        }
    }


I do not have a lot experience with Java, so i tested your code (with small changes) in C#:

for(int abc=100;abc<1000;abc++)
{
    int a=abc/100;
    int b=abc%100/10;
    int c=abc%10;
    int sum=a+b+c;
    int sum2 = (int)(System.Math.Pow(a,2) + System.Math.Pow(b,2) + System.Math.Pow(c,2));
    if(sum==sum2)
    {
        Console.WriteLine("abc={0} a={1} b={2} c={3} sum={4} sum2={5}", abc, a, b, c, sum, sum2);
    }
}




and using Linq query too:

var qry = Enumerable.Range(100, 900).
            Select(z=> new{a = (int)z/100, b = (int)z%100/10, c = (int)z%10}).
            Select(n=> new{a = n.a, b = n.b, c = n.c, sum = n.a+n.b+n.c, sum2 = (int)(Math.Pow(n.a,2) + Math.Pow(n.b,2) + Math.Pow(n.c,2))}).
            Where(x=>x.sum==x.sum2).
            Select(f=>f);



Result is the same:

a b c sum sum2
1 0 0 1 1
1 0 1 2 2
1 1 0 2 2
1 1 1 3 3


这篇关于求abc是a2 + b2 + c2 = a + b + c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆