110703 Euclid problem


import java.util.*;

public class Main {
	
	private static void EuclidAlgorithm(int a, int b)
	{
		if (a < b)
		{
			EuclidAlgorithm(b, a);
			int temp = ResultX;
			ResultX = ResultY;
			ResultY = temp;
			return;
		}
		
		if (b == 0)
		{
			ResultX = 1;
			ResultY = 0;
			ResultGCD = a;
			return;
		}
		
		int floor = a / b;
		EuclidAlgorithm(b, a - b * floor);
		int tempX = ResultX;
		int tempY = ResultY;
		ResultX = tempY;
		ResultY = tempX - tempY * floor;
		return;
	}
	
	private static void OutputEuclidNumbers(int a, int b)
	{
		EuclidAlgorithm(a, b);
		System.out.println(ResultX + " " + ResultY + " " + ResultGCD);
	}
	
	public static void main(String[] args){
		Scanner inScanner = new Scanner(System.in);
		while(inScanner.hasNextInt())
		{
			int a = inScanner.nextInt();
			int b = inScanner.nextInt();
			OutputEuclidNumbers(a, b);
		}
	}
	
	private static int ResultX;
	private static int ResultY;
	private static int ResultGCD;
}


阅读更多
文章标签: 编程
个人分类: 算法练习
上一篇110706 Smith Numbers
下一篇110707 Marbles
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭