Cod sursa(job #1693378)

Utilizator sdwolfSDWOLF sdwolf Data 22 aprilie 2016 23:13:38
Problema Algoritmul lui Euclid Scor 100
Compilator c Status done
Runda Arhiva educationala Marime 0.64 kb
#include <stdio.h>

long cmmdc(long first, long second);

int main() {
  FILE *input, *output;
  long count, first, second, result;

  input  = fopen("euclid2.in",  "r");
  output = fopen("euclid2.out", "w");

  fscanf(input, "%ld\n", &count);

  for(long index = 0; index < count; index += 1) {
    fscanf(input, "%ld %ld\n", &first, &second);

    result = cmmdc(first, second);

    fprintf(output, "%ld\n", result);
  }

  fclose(input);
  fclose(output);

  return 0;
}

long cmmdc(long first, long second) {
  long reminder = 0;

  while (second) {
    reminder = first % second;
    first    = second;
    second   = reminder;
  }

  return first;
}