Advertisement
AdrianMadajewski

Okregi Maturka

Feb 4th, 2019
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <fstream>
  4. #include <vector>
  5. #include <climits>
  6. #include <cmath>
  7. #include <utility>
  8.  
  9. struct Okrag
  10. {
  11.     int x;
  12.     int y;
  13.     int r;
  14. };
  15.  
  16. // Czy okrag o1 zawiera sie w o2
  17. bool czyNalezy(Okrag o1, Okrag o2)
  18. {
  19.     // Rownanie o2
  20.     // (x - o2.x)^2 - (x - o2.y) ^ 2 = o2.r^2
  21.     return pow(o1.x - o2.x, 2) + pow(o1.y - o2.y, 2) <= pow(o2.r, 2);
  22. }
  23.  
  24. bool warunek(Okrag o1, Okrag o2)
  25. {
  26.     bool w1 = pow(o1.r + o2.r, 2) == pow(o1.x - o2.x, 2) + pow(o1.y - o2.y, 2);
  27.     bool w2 = pow(o1.r - o2.r, 2) == pow(o1.x - o2.x, 2) + pow(o1.y - o2.y, 2);
  28.     return w1 || w2;
  29. }
  30.  
  31. int main()
  32. {
  33.     std::vector<Okrag> tab;
  34.     std::ifstream dane("dane.txt");
  35.  
  36.     unsigned int N = 100;
  37.     for(int i = 0; i < N; i++)
  38.     {
  39.         Okrag tmp;
  40.         dane >> tmp.x;
  41.         dane >> tmp.y;
  42.         dane >> tmp.r;
  43.  
  44.         tab.emplace_back(tmp);
  45.     }
  46.  
  47.     Okrag o1;
  48.     o1.r = INT_MAX;
  49.     o1.x = 0;
  50.     o1.y = 0;
  51.  
  52.     std::vector<Okrag> maleR;
  53.  
  54.     for (auto &temp : tab)
  55.     {
  56.         if (temp.r < o1.r)
  57.         {
  58.             o1 = temp;
  59.             maleR.clear();
  60.         }
  61.         if (temp.r == o1.r)
  62.             maleR.emplace_back(temp);
  63.     }
  64.  
  65.     // 4.1
  66.     std::ofstream wynik1("wynik1.txt");
  67.     for (auto &okrag : maleR)
  68.         wynik1 << okrag.x << " " << okrag.x << " " << okrag.r << std::endl;
  69.     wynik1 << maleR.size();
  70.  
  71.  
  72.     // 4.2
  73.     std::ofstream wynik2("wynik2.txt");
  74.  
  75.     int ile_srodkow = 0;
  76.     int max_srodkow = INT_MIN;
  77.     Okrag maxokrag;
  78.  
  79.     for (auto &okrag : tab)
  80.     {
  81.         for (auto &test : tab)
  82.         {
  83.             if (czyNalezy(test, okrag))
  84.                 ile_srodkow++;
  85.         }
  86.  
  87.         if (ile_srodkow > max_srodkow)
  88.         {
  89.             maxokrag = okrag;
  90.             max_srodkow = ile_srodkow;
  91.         }
  92.         ile_srodkow = 0;
  93.     }
  94.  
  95.     // max_srodkow - 1 bo sprawdzam kazdy a kazdym, a nie kazdy z kolejnym
  96.     wynik2 << maxokrag.x << " " << maxokrag.y << " " << maxokrag.r << std::endl;
  97.     wynik2 << max_srodkow - 1;
  98.  
  99.     // 4.3
  100.     std::ofstream wynik3("wynik3.txt");
  101.     int ile_par = 0;
  102.  
  103.     std::vector<std::pair<Okrag, Okrag>> pary;
  104.  
  105.     for (int i = 0; i < tab.size(); i++)
  106.     {
  107.         for (int j = i + 1; j < tab.size(); j++)
  108.         {
  109.             if (warunek(tab[i], tab[j]))
  110.             {
  111.                 ile_par += 2;
  112.                 pary.emplace_back(tab[i], tab[j]);
  113.                 pary.emplace_back(tab[i], tab[j]);
  114.             }
  115.         }
  116.     }
  117.    
  118.     wynik3 << ile_par << std::endl;
  119.     for (auto &para : pary)
  120.     {
  121.         wynik3 << para.first.x << " " << para.first.y << " " << para.first.r << " ";
  122.         wynik3 << para.second.x << " " << para.second.y << " " << para.second.r << std::endl;
  123.     }
  124.     return 0;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement