欧几里得算法

package euclidean_algorithm;

import java.util.Scanner;

/**
* @author ALazy_cat
* 欧几里得算法的自然语言描述:
* 计算两个非负整数x和y的最大公约数: 若y = 0,则最大公约数为x; 否则将remainder = x % y,x和y的
* 最大公约数即为y和remainder的最大公约数
*/
public class EuclideanAlgorithm {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.print("请输入两个整数: ");
int x = 0, y = 0;
x = in.nextInt();
y = in.nextInt();
System.out.println("x, y的最大公约数是: " + euclideanAlgorithm_01(x, y, 1));
System.out.println("---------------------");
System.out.println("x, y的最大公约数是: " + euclideanAlgorithm_02(x, y, 1));
} //欧几里得算法的递归实现
public static int euclideanAlgorithm_01(int x, int y, int count) {
//当y = 0时,递归结束
int remainder = 0;
System.out.println("第" + count++ + "次递归: " + "x = " + x + " , " + "y = " + y);
if (y == 0)
return x;
remainder = x % y;
return euclideanAlgorithm_01(y, remainder, count);
} //欧几里得算法的循环实现
public static int euclideanAlgorithm_02(int x, int y, int count) {
int remainder = 0;
while (y != 0) {
System.out.println("第" + count++ + "次循环: " + "x = " + x + " , " + "y = " + y);
remainder = x % y;
x = y;
y = remainder;
}
System.out.println("第" + count++ + "次循环: " + "x = " + x + " , " + "y = " + y);
return x;
}
}

最新文章

  1. [C1] 分离 C1FlexGrid 滚动条
  2. openstack想说爱你不容易
  3. Sql Server 分区演练 【转】
  4. SQL update join on 连接更新
  5. Ubuntu14.04环境下Samba报错排错过程
  6. 转:SSL协议详解
  7. 从OGRE,GAMEPLAY3D,COCOS2D-X看开源
  8. 高质量、处于持续更新的R包
  9. Android学习5—布局简介
  10. 通过ctypes获得python windows process的内存使用情况
  11. zf-关于把某个地址的svn项目移动到另一个上面的步骤
  12. eclipse创建Maven父子结构Maven项目
  13. Problem Q
  14. expect 简单使用
  15. 【UOJ448】【集训队作业2018】人类的本质 min_25筛
  16. HomeBrew及HomeBrew Cask的简介和使用
  17. HTML5:一些部件
  18. 阿里云 CentOS安装Git
  19. Azkaban介绍+安装部署+实战案例
  20. 跟着老王学Python

热门文章

  1. 2019.3.16数据结构考试(Problem 1. rotinv)(循环逆序对)
  2. CentOS 7 + MySql 中文乱码解决方案
  3. PAT 1152 Google Recruitment
  4. 网络爬虫&起点中文网完本榜500部小说
  5. gitlab安装随记
  6. python中pyperclip库的功能
  7. springboot秒杀课程学习整理1-6
  8. csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理
  9. MapReduce实现Apriori算法
  10. [Linux] Extend space of root disk in Linux7