Hypereq

XD

Jan 20th, 2023
985
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <iostream>        
  2. #include <stdio.h>          
  3. #include <math.h>
  4. #include <windows.h>
  5. #include <string.h>
  6. #include <vector>
  7. #include <algorithm>
  8. #include <fstream>
  9. #include <string>
  10. #include <sstream>
  11. #include <cmath>
  12. #include <utility>
  13. #include <map>
  14.  
  15. using namespace std;
  16.  
  17. void wyswietlWektor(vector<int> &wektor)
  18. {
  19.     cout << endl << "INSTANCJA:         DLUGOSC INSTANCJI: " << wektor.size() << endl;
  20.  
  21.     for(int i = 0; i < wektor.size(); i++)
  22.     {
  23.         cout << wektor[i] << " ";
  24.     }
  25. }
  26.  
  27. void wczytaj(vector<int> &wczytanaInstancja)
  28. {
  29.     string fileName;
  30.     string fullFileName;
  31.     cout << endl << "PODAJ NAZWE PLIKU (BEZ .TXT): ";
  32.     getline(cin >> ws, fileName);
  33.     fullFileName = fileName + ".txt";
  34.     ifstream instancjaMapy(fullFileName);
  35.  
  36.     if(!instancjaMapy.is_open())
  37.     {
  38.         cout << "BLAD";
  39.         exit(1);
  40.     }
  41.  
  42.     string linia;
  43.     string liczba;
  44.  
  45.     getline(instancjaMapy, linia);
  46.     stringstream podzieloneLiczby(linia);                                          
  47.  
  48.     while(podzieloneLiczby >> liczba)
  49.     {
  50.         wczytanaInstancja.push_back(stoi(liczba));
  51.     }
  52. }
  53.  
  54. long long int silnia(int liczba)
  55. {
  56.     long long int wynik = 1;
  57.  
  58.     for(int i = 1; i <= liczba; i++)
  59.     {
  60.         wynik = wynik * i;
  61.     }
  62.  
  63.     return wynik;
  64. }
  65.  
  66. long long int symbolNewtona(int gornaLiczba, int dolnaLiczba)
  67. {
  68.     return silnia(gornaLiczba) / (silnia(dolnaLiczba) * silnia(gornaLiczba - dolnaLiczba));
  69. }
  70.  
  71. int sprawdzenieDlugosci(vector<int> &wczytanaInstancja)
  72. {
  73.     int dlugoscInstancji = wczytanaInstancja.size();
  74.  
  75.     for(int k = 1; k < 20; k++)
  76.     {
  77.         if(symbolNewtona(k+2, 2) == dlugoscInstancji)
  78.         {
  79.             return k;
  80.         }
  81.     }
  82.  
  83.     return 0;
  84. }
  85.  
  86. int obliczRoznice(vector<int> &wczytanaInstancja, int &roznica)
  87. {
  88.     int max1 = wczytanaInstancja[0];
  89.     int max2 = wczytanaInstancja[0];
  90.  
  91.     for(auto i: wczytanaInstancja)
  92.     {
  93.         if(i > max1)
  94.         {
  95.             max2 = max1;
  96.             max1 = i;
  97.         }
  98.         else if(i > max2)
  99.         {
  100.             max2 = i;
  101.         }
  102.     }
  103.  
  104.     cout << endl << "PIERWSZA NAJWIEKSZA: " << max1;
  105.     cout << endl << "DRUGA NAJWIEKSZA: " << max2;
  106.  
  107.     roznica = max1 - max2;
  108.     return roznica;
  109. }
  110.  
  111. // void szukaj(int roznica,int ind,int &czyJest)
  112. // {
  113. //    //...
  114.  
  115. //    if(ind==maxind)
  116. //    {
  117. //       <wypisz rozwiązanie>
  118. //       czyJest=1;
  119. //    }
  120. //    else // for (...)
  121. //    {
  122. //         //...
  123. //         if(<warto kontynuować bieżące rozwiązanie>)
  124. //         {
  125. //             szukaj(roznica,ind+1,czyJest);
  126. //         }
  127. //         if(czyJest==1)
  128. //         {
  129. //             break;
  130. //         }
  131. //         //...
  132. //    }
  133. // }
  134.  
  135. int main()
  136. {
  137.     int ind = 0;
  138.     int czyJest = 0;
  139.     int roznica = 0;
  140.     int liczbaCiec = 0;
  141.     vector<int> wczytanaInstancja;
  142.     wczytaj(wczytanaInstancja);
  143.     wyswietlWektor(wczytanaInstancja);
  144.    
  145.     liczbaCiec = sprawdzenieDlugosci(wczytanaInstancja);
  146.  
  147.     if(liczbaCiec == 0)
  148.     {
  149.         cout << endl << "DLUGOSC INSTANCJI JEST NIEPOPRAWNA";
  150.         exit(1);
  151.     }
  152.     else
  153.     {
  154.         cout << endl << "LICZBA CIEC: " << liczbaCiec;
  155.     }
  156.  
  157.     obliczRoznice(wczytanaInstancja, roznica);
  158.  
  159.     cout << endl << "ROZNICA: " << roznica;
  160.  
  161.     // szukaj(roznica,ind+1,czyJest);
  162.  
  163.     if(czyJest == 0)
  164.     {
  165.         cout << endl << "NIE MA ROZWIAZANIA";
  166.     }
  167.  
  168.  
  169.    
  170.  
  171.     cout << endl << "DZIALA JAK NARAZIE";
  172.  
  173.     return 0;
  174. }
Advertisement
Add Comment
Please, Sign In to add comment