소 감 ¶
ver.1 ¶
if(x1 < x2) // 이 부분이 꼭 필요하지 않다고 문득 느낌..
{
int temp;
temp = x1;
x1 = x2;
x1 = temp;
}temp = x1;
x1 = x2;
x1 = temp;
ver.2 ¶
이거 짜면서 ver.1까지 새로 짰다.
잘못된 변수로 print한 것을 발견..
나는 후자를 선택했다.
잘못된 변수로 print한 것을 발견..
- 처음부터 x,y값을 프린트하고 그 값을 계속 쓰는 방법, 그리고
나는 후자를 선택했다.
소스 ¶
ver.1 ¶
~cpp
#include <stdio.h>
void main()
{
int x2, y2, x, y,remainder;
printf("두 숫자 입력 (ex| 5 6):");
scanf("%d %d", &x, &y);
x2 = x;
y2 = y; //y값을 y2를 이용해 사용.
if(y2 < x2)
{
int temp;
temp = x2;
x2 = y2;
x2 = temp;
}
// 유클리드 호제법
while(y2 != 0){
remainder = x2 % y2; // x2= 나눠질 값. y2= 나누는 값. remainder= 나머지.
x2 = y2;
y2 = remainder;
}
printf("x = %d, y = %d\n GCD is %d", x, y, x2);
}
ver.2 ¶
~cpp
#include <stdio.h>
void Eu_clidian(int x, int y);
void main()
{
int x, y;
printf("두 숫자 입력 (ex| 5 6):");
scanf("%d %d", &x, &y);
Eu_clidian(x, y);
}
void Eu_clidian(int x, int y)
{
int x2, y2, remainder;
x2 = x;
y2 = y;
while(y2 != 0){ // 유클리드 호제법
remainder = x2 % y2;
x2 = y2;
y2 = remainder;
}
printf("The GCD of %d and %d is %d\n", x, y, x2);
}










