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.0056 sec