U E D R , A S I H C RSS

Euclid Problem/조현태

느낀점 및 설명

고등학교.. 아니 중학교때 부터 느꼈지만.. 유클리드 아저씨..정말 밉다..ㅠ.ㅜ
최대공약수는 저번에 만들어 놓은 소스.. 복사~붙여넣기.. 이걸로 해결~
그러나 수학적 지식의 부재로 x,y는 어떻게 구해야 할지 모르겠다는 판단하에..
내가 수학시간에 주로 써먹었던.. 대입법! (이름만 거창하지 적당히 찍어서 넣어본다라는 이야기..)
문제에서 주어진 규칙을 만족하는 숫자가 나오도록 잔머리 굴려서 대입시키도록 해놓았다.^^;
... 대입법도 훌륭한 풀이다 뭐...ㅠ.ㅜ....

소스

~cpp 
#include <stdio.h>

int Get_GCM(int , int );
void Get_x_y(int, int, int*, int*, int );

void main()
{
	int input_a, input_b;
	while (1)
	{
		int x=0, y=0, gcm=0;
		printf ("두 숫자를 입력해 주세요.(0,0)은 정지\n>>");
		scanf ("%d%d",&input_a,&input_b);
		if (0==input_a && 0==input_b)
			break;
		gcm=Get_GCM(input_a, input_b);
		Get_x_y(input_a, input_b, &x, &y, gcm);
		printf ("결과 : x=%d\ty=%d\tGCM=%d\n",x,y,gcm);
	}
}

void Get_x_y(int number_a, int number_b, int* x, int* y, int gcm)
{
	int *temp_large, *temp_small, temp_plus=1;
	if (number_a>number_b)
	{
		temp_large=x;
		temp_small=y;
	}
	else
	{
		temp_large=y;
		temp_small=x;
	}
	while(number_a*(*x)+number_b*(*y)!=gcm)
	{
		if (temp_plus>0 && number_a*(*x)+number_b*(*y)>gcm)
		{
			*temp_large=0;
			temp_plus=-1;
		}
		else if (temp_plus<0 && number_a*(*x)+number_b*(*y)<gcm)
		{
			*temp_large=0;
			temp_plus=1;
			if (*temp_small>0)
				*temp_small*=-1;
			else
				*temp_small=*temp_small*(-1)+1;
		}
		else
			*temp_large+=temp_plus;
	}
}

int Get_GCM(int number_a, int number_b)
{
	int temp;
	while (temp=number_a%number_b)
	{
		number_a=number_b;
		number_b=temp;
	}
	return number_b;
}

저에게 할말

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:23:14
Processing time 0.0083 sec