Advertisement
Guest User

NWD SPOJ

a guest
Jan 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.38 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3.  
  4. int nwd(int a, int b)
  5. {
  6.     int c;
  7.     do
  8.     {
  9.         if (a % b == 0) return b;
  10.         c = a % b;
  11.         if (b % c == 0) return c;
  12.         a = b % c;
  13.         if (c % a == 0) return a;
  14.     } while (a % b != 0);
  15. }
  16.  
  17. int main()
  18. {
  19.     int t, a, b;
  20.     std::cin >> t;
  21.     for (int i = 1; i < t+1; i++)
  22.     {
  23.         std::cin >> a >> b;
  24.         std::cout << nwd(a , b) <<"\n";
  25.     }
  26.     return 0;
  27. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement