Created
May 4, 2015 11:23
-
-
Save rogerioagjr/0ee00e50200be36a8373 to your computer and use it in GitHub Desktop.
Mini-Calculadora
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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