Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4.  
  5. int nwd(int ,int);
  6.  
  7. int main()
  8. {
  9. int testy, i, a, b;
  10. cin >> testy;
  11. for(i=1; i<=testy; i++)
  12. {
  13. cin >> a >> b;
  14. cout << nwd(a, b) << endl;
  15. }
  16.  
  17. return 0;
  18. }
  19.  
  20. int nwd(int a, int b)
  21. {
  22. int c;
  23. if (((a==b || a==-b)) && ((a || b) != 0))
  24. return abs(a); // a lub b moga byc ujemne, a NWD nigdy nie jest ujemny, wiec daje po prostu abs()
  25.  
  26. else if (a==0 || b==0)
  27. return 0;
  28.  
  29. else
  30. {
  31. if (b>a)
  32. {
  33. c=a; a=b; b=c; // c jest tutaj tylko po to zeby zapisac a do b, b do a
  34. }
  35.  
  36. while(c != 0)
  37. {
  38. c = a%b; // tutaj juz c przyda sie nam do czegos innego
  39. if (c == 0)
  40. return b;
  41.  
  42. else
  43. a=b; b=c;
  44. }
  45. }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement