Skip to content

Instantly share code, notes, and snippets.

@rogerioagjr
Created May 4, 2015 11:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rogerioagjr/0ee00e50200be36a8373 to your computer and use it in GitHub Desktop.
Save rogerioagjr/0ee00e50200be36a8373 to your computer and use it in GitHub Desktop.
Mini-Calculadora
#include <cstdio>
int n, d, q, x, y;
int main(){
scanf("%d %d %d", &n, &d, &q); // leio os valores de n, d, e q
x=d; y=q; // faço as auxiliares receberem, inicialmente, os valores de d e q
// garanto que y é o menor
if(x<y){
int tmp=x;
y=y%x;
x=tmp;
}
// algoritmo de Euclides para encontrar o MDC
while(y!=0){ // enquanto o menor for diferente de 0
// faço o mdc entre o menor e o resto que o maior deixa na divião pelo menor
if(x<y){
int tmp=x;
y=y%x;
x=tmp;
}
else{
int tmp=y;
y=x%y;
x=tmp;
}
}
// ao fim do algoritmo, x será o MDC
d/=x; q/=x; // divido os dois números pelo MDC
if(d<n && q<n) printf("%d %d\n", d, q); // se os números não sperarem n, os imprimo
else printf("IMPOSSIVEL\n"); // se não, imprimo impossível
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment