Find Pythagorean triplet for which a + b + c = 1000

后端 未结 16 2685
离开以前
离开以前 2020-12-24 13:13

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a2 + b2 = c2

For example, 32 + 4

16条回答
  •  长情又很酷
    2020-12-24 14:05

    #include 
    
    int main() // main always returns int!
    {
     int a, b, c;
     for (a = 0; a<=1000; a++)
     {
      for (b = a + 1; b<=1000; b++) // no point starting from 0, otherwise you'll just try the same solution more than once. The condition says a < b < c.
      {
       for (c = b + 1; c<=1000; c++) // same, this ensures a < b < c.
       {
        if (((a*a + b*b == c*c) && ((a+b+c) ==1000))) // ^ is the bitwise xor operator, use multiplication for squaring
         printf("a=%d, b=%d, c=%d",a,b,c);
       }
      }
     }
     return 0;
    }
    

    Haven't tested this, but it should set you on the right track.

提交回复
热议问题