Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <vector>
- #include <iostream>
- using namespace std;
- // ------------------------- Deklaracje funkcji ------------------------- //
- int nwd (int a, int b);
- // ------------------------- main ------------------------- //
- int main()
- {
- // ------------------------- Pobranie danych ------------------------- //
- int t {};
- cin >> t;
- vector <vector <int> > a;
- a.resize(t);
- for (int i=0 ; i<t ; i++)
- {
- a[i].resize(2);
- cin >> a[i][0];
- cin >> a[i][1];
- }
- // ------------------------- Szeregowanie danych ------------------------- //
- int bufor {};
- for (int i=0 ; i<t ; i++) // Szeregujemy tablice.
- { // Wiekszy element rzedu ma byc na indeksie [i][0]
- if ( a[i][1] > a[i][0] ) // a mniejszy - [i][1]
- {
- bufor = a[i][0];
- a[i][0] = a[i][1];
- a[i][1] = bufor;
- }
- }
- // ------------------------- Wypisanie wynikow ------------------------- //
- for (int i=0 ; i<t ; i++)
- {
- cout << nwd(a[i][0], a[i][1]);
- if (i<t-1) cout << endl;
- }
- // ------------------------- Koniec ------------------------- //
- }
- // ------------------------- nwd() ------------------------- //
- int nwd (int a, int b)
- {
- int rezultat {};
- rezultat = b; // Wiemy juz, ze b <= a
- if ( (a % b) ) // Jesli a dzieli sie przez b, pomin ponizszy blok.
- {
- if ( b % 2 == 0) { b /= 2; rezultat = b; } // Najpierw sprobuj podzielic b przez 2.
- while((a % rezultat) or (b % rezultat)) // Tak dlugo, az zarowno a jak i b nie beda
- { // podzielne przez rezultat,
- rezultat--; // pomniejszaj rezultat.
- }
- }
- return rezultat;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement