Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- #include <climits>
- struct Punct
- {
- int X, Y;
- void citirePunct()
- {
- std::cin >> X >> Y;
- }
- float distanta(Punct P)
- {
- return sqrt((P.X - X) * (P.X - X) + (P.Y - Y) * (P.Y - Y));
- }
- };
- struct PerechePuncte
- {
- Punct P1, P2;
- };
- void citire(Punct*& vectorPuncte, int& nrPuncte)
- {
- std::cin >> nrPuncte;
- vectorPuncte = new Punct[nrPuncte];
- for (int index = 0; index < nrPuncte; ++index)
- vectorPuncte[index].citirePunct();
- }
- void atribuire(PerechePuncte& pereche, Punct Punct1, Punct Punct2)
- {
- pereche.P1.X = Punct1.X, pereche.P1.Y = Punct1.Y;
- pereche.P2.X = Punct2.X, pereche.P2.Y = Punct2.Y;
- }
- void perechi(Punct* vectorPuncte, int nrPuncte, PerechePuncte& apropiata, PerechePuncte& departata)
- {
- float distMin = FLT_MAX, distMax = 0;
- for (int i = 0; i < nrPuncte - 1; ++i)
- for (int j = i + 1; j < nrPuncte; ++j)
- {
- float dist = vectorPuncte[i].distanta(vectorPuncte[j]);
- if (dist < distMin)
- {
- distMin = dist;
- atribuire(apropiata, vectorPuncte[i], vectorPuncte[j]);
- }
- if (dist > distMax)
- {
- distMax = dist;
- atribuire(departata, vectorPuncte[i], vectorPuncte[j]);
- }
- }
- }
- void afisarePereche(PerechePuncte pereche)
- {
- std::cout << "{(" << pereche.P1.X << ", " << pereche.P1.Y << "), ";
- std::cout << "(" << pereche.P2.X << ", " << pereche.P2.Y << ")}\n";
- }
- int main()
- {
- int nrPuncte;
- Punct* vectorPuncte;
- PerechePuncte apropiata, departata;
- citire(vectorPuncte, nrPuncte);
- perechi(vectorPuncte, nrPuncte, apropiata, departata);
- std::cout << "Perechea de puncte cele mai apropiate este: ";
- afisarePereche(apropiata);
- std::cout << "Perechea de puncte aflate la cea mai mare distanta unul fata de celelalt este: ";
- afisarePereche(departata);
- delete[]vectorPuncte;
- return 0;
- }
Add Comment
Please, Sign In to add comment