Advertisement
thinhckhcmus

UCLN

Aug 12th, 2019
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. int gcd(int a, int b){
  2. // Nếu a = 0 => ucln(a,b) = b
  3. // Nếu b = 0 => ucln(a,b) = a
  4. if (a == 0 || b == 0){
  5. return a + b;
  6. }
  7. while (a != b){
  8. if (a > b){
  9. a -= b; // a = a - b
  10. }else{
  11. b -= a;
  12. }
  13. }
  14. return a; // return a or b, bởi vì lúc này a và b bằng nhau
  15. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement