关闭

110703 Euclid problem

标签: 编程
310人阅读 评论(0) 收藏 举报
分类:


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;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:21483次
    • 积分:733
    • 等级:
    • 排名:千里之外
    • 原创:55篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类