Nerde Nolzda

ZeroJudge: a024 最大公因數 (GCD)

輾轉相除法
Use the Euclidean algorithm.

ZeroJudge Link (Zh)

#include <stdio.h>

main() {
  int a, b;
  while (scanf("%d %d", &a, &b) != EOF) {
    while (b != 0) {
      if (a < b) { a ^= b; b ^= a; a ^= b; }
      a -= b;
    }
    printf("%d\n", a);
  }
  return 0;
}

Related Posts

0 comments

Post a comment

Send an email to comment@nerde.pw.