mardi 19 février 2019

How can I print out the perimeter and the number of right-angled triangle with a given range?

The question need the user input two value, P and Q. The program then will output the number of right angle integer triangle as well as its perimeter from P to Q. For example:

Input: 154 180

Output:

154 1

156 1

160 1

168 3

176 1

180 3

I think i need to find out the Pythagorean Triples in the P-Q range, but how to count the " number of right-angled triangle " ? Here are my code :

#include <iostream>
#include <math.h>
using namespace std;
int main() {
int P, Q, a, b, c, i = 0;
cin >> P >> Q;
for ( a = P; a <= Q; ++a)
{
   for ( b = a; b <= Q; ++b)
   {
      for ( c = b; b <= Q; ++c)
      {
         if ((pow(a, 2) + pow(b, 2)) == pow(c, 2) && a + b + c <= Q)
         {
             i +=1;
             cout << a + b + c << " " << i << endl;
             }
      }
    }
}
return 0;
}

Super Thanks !!

Aucun commentaire:

Enregistrer un commentaire