Untitled

mail@pastecode.io avatar
unknown
c_cpp
a year ago
2.5 kB
1
Indexable
Never
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <vector>

using namespace std;

// struct qui represente les solutions trouvées
struct sol {
    int a, b, c, d;
    bool operator<(const sol& other) const {
        if (a != other.a) return a < other.a;
        if (b != other.b) return b < other.b;
        if (c != other.c) return c < other.c;
        return d < other.d;
    }
};

int main() {
    // lecture des données
    int A, B;
    cin >> A >> B;

    // vecteur des solutions
    vector<sol> solutions;

    // vérifier toutes les combinaisons de a et b telles que 1 <= a <= A et 1 <= b <= B
    for (int a = 1; a <= A; a++) {
        for (int b = a; b <= B; b++) {
            // si a et b sont impairs, prochaine itération
            if (a % 2 != 0 && b % 2 != 0)
                continue;

            // les carrés et a et b
            int a2 = a * a;
            int b2 = b * b;

            int p = 2;
            while ((p*p) < (a2 + b2)) {
                if ((a2+b2)%p == 0) {
                    int p2 = p * p; // p carré
                    int c = (a2 + b2 - p2) / (2 * p); // calcul de c
                    int d = (a2 + b2 + p2) / (2 * p); // calcul de d

                    // trier les 3 valeurs afin d'avoir a <= b <= c
                    vector<int> abc;
                    abc.push_back(a);
                    abc.push_back(b);
                    abc.push_back(c);
                    sort(abc.begin(), abc.end());

                    // sauvegarder la solution
                    struct sol s;
                    s.a = abc[0];
                    s.b = abc[1];
                    s.c = abc[2];
                    s.d = d;
                    if (s.a <= s.b && s.b <= s.c && s.c <= s.d && ((s.a*s.a)+(s.b*s.b)+(s.c*s.c)==(s.d*s.d)))
                        solutions.push_back(s);
                }
                p++;
            }
        }
    }
    // simplification
    sort(solutions.begin(), solutions.end(), [](sol a, sol b){return a.a < b.a;});
    // set afin d'avoir des solutions uniques
    set<sol> solutionsUniques;
    for (auto& el : solutions) {
        solutionsUniques.insert(el);
    }
    // afficher les solutions
    for (auto el : solutionsUniques) {
        if (el.a > 0 && el.a <= A && el.b > 0 && el.b <= B)
            cout << el.a << " " << el.b << " " << el.c << " " << el.d << endl;
    }


    return 0;
}