Advertisement
Guest User

Untitled

a guest
Feb 28th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.90 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. using namespace std;
  4.  
  5. int NW1D(int a, int b)
  6. {
  7.     if(a==1 || b==1) return 1;
  8.  
  9.     if(a == b) return a;
  10.     else if(a > b) return NW1D(a - b, b);
  11.     else if(a < b) return NW1D(a, b - a);
  12.    
  13. }
  14.  
  15. int NWD(int a, int b)
  16. {
  17.     while(a != b && a != 1 && b != 1)
  18.     {
  19.         if(a > b) a-=b;
  20.         else b-=a;
  21.     }
  22.  
  23.     return a;
  24. }
  25.  
  26. void _4_3()
  27. {
  28.     ifstream dane("przyklad.txt");
  29.  
  30.     int tab[500];
  31.  
  32.     for(int i=0; i<500; i++)
  33.     {
  34.         dane >> tab[i];
  35.     }
  36.  
  37.     int tmp_licznik = 0;
  38.     int max_licznik = 0;
  39.     int max_nwd = 0;
  40.     int tmp_nwd= 0;
  41.     int pierwsza= 0;
  42.     int max_pierwsza = 0;
  43.    
  44.     for(int i=0; i<500; i++)
  45.     {
  46.         tmp_licznik = 1;
  47.         tmp_nwd = 0;
  48.  
  49.         for(int j=i; j<499; j++)
  50.         {
  51.             if((NWD(tab[j], tab[j+1])) > 1 && tmp_licznik == 1)
  52.             {
  53.                 tmp_nwd = NWD(tab[j], tab[j+1]);
  54.                 tmp_licznik++;
  55.                 pierwsza = tab[j];
  56.                 cout << "poczatek\n" << tmp_licznik << " " << pierwsza <<" aktualna: "<<tab[j+1] << endl;
  57.             }
  58.             else if(NWD(tmp_nwd, tab[j+1]) > 1)
  59.             {
  60.                 tmp_nwd = NWD(tmp_nwd, tab[j+1]);
  61.                 tmp_licznik++;
  62.                 cout << tmp_licznik << " " << pierwsza << "aktualna: " <<tab[j+1] << endl;
  63.                
  64.             }
  65.             else
  66.             {
  67.                  cout << "break::  " << tmp_licznik << " " << pierwsza <<"ostatnia: "<<tab[j] << endl;
  68.                 break;
  69.             }
  70.         }
  71.         cout << "\n";
  72.         if(tmp_licznik > max_licznik)
  73.         {
  74.             max_licznik = tmp_licznik;
  75.             max_nwd = tmp_nwd;
  76.             max_pierwsza = pierwsza;
  77.         }
  78.     }
  79.  
  80.     cout << max_licznik << endl;
  81.     cout << max_nwd << endl;
  82.     cout << max_pierwsza;
  83. }
  84.  
  85. int main()
  86. {
  87.     _4_3();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement