열렬히.뛰기

pgcd

école 42 > exam02 > exam02 : part3 > pgcd

푸는 법

💡 유클리드 호제법 : GCD(a, b) = GCD(b, a % b)

유클리드 호제법을 이용해서 푼다.

n이 0이 되면 탈출, 이 때의 a가 바로

c
int a = (큰 수)
int b = (작은 수)
int n = 0;

while (b != 0)
{
	n = a % b;
	a = b;
	b = n;
}

printf("%d", a);

문제

Assignment name  : pgcd
Expected files   : pgcd.c
Allowed functions: printf, atoi, malloc, free
--------------------------------------------------------------------------------

Write a program that takes two strings representing two strictly positive
integers that fit in an int.

Display their highest common denominator followed by a newline (It's always a
strictly positive integer).

If the number of parameters is not 2, display a newline.

Examples:

$> ./pgcd 42 10 | cat -e
2$
$> ./pgcd 42 12 | cat -e
6$
$> ./pgcd 14 77 | cat -e
7$
$> ./pgcd 17 3 | cat -e 
1$
$> ./pgcd | cat -e
$