Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cmath>
- using namespace std;
- int nwd(int ,int);
- int main()
- {
- int testy, i, a, b;
- cin >> testy;
- for(i=1; i<=testy; i++)
- {
- cin >> a >> b;
- cout << nwd(a, b) << endl;
- }
- return 0;
- }
- int nwd(int a, int b)
- {
- int c;
- if (((a==b || a==-b)) && ((a || b) != 0))
- return abs(a); // a lub b moga byc ujemne, a NWD nigdy nie jest ujemny, wiec daje po prostu abs()
- else if (a==0 || b==0)
- return 0;
- else
- {
- if (b>a)
- {
- c=a; a=b; b=c; // c jest tutaj tylko po to zeby zapisac a do b, b do a
- }
- while(c != 0)
- {
- c = a%b; // tutaj juz c przyda sie nam do czegos innego
- if (c == 0)
- return b;
- else
- a=b; b=c;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement