简单学完HTML+CSS+JS,现在开始看算法(第四版)----欧几里得算法
2024-10-12 01:11:48
欧几里得算法
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;
}
}
最新文章
- [C1] 分离 C1FlexGrid 滚动条
- openstack想说爱你不容易
- Sql Server 分区演练 【转】
- SQL update join on 连接更新
- Ubuntu14.04环境下Samba报错排错过程
- 转:SSL协议详解
- 从OGRE,GAMEPLAY3D,COCOS2D-X看开源
- 高质量、处于持续更新的R包
- Android学习5—布局简介
- 通过ctypes获得python windows process的内存使用情况
- zf-关于把某个地址的svn项目移动到另一个上面的步骤
- eclipse创建Maven父子结构Maven项目
- Problem Q
- expect 简单使用
- 【UOJ448】【集训队作业2018】人类的本质 min_25筛
- HomeBrew及HomeBrew Cask的简介和使用
- HTML5:一些部件
- 阿里云 CentOS安装Git
- Azkaban介绍+安装部署+实战案例
- 跟着老王学Python
热门文章
- 2019.3.16数据结构考试(Problem 1. rotinv)(循环逆序对)
- CentOS 7 + MySql 中文乱码解决方案
- PAT 1152 Google Recruitment
- 网络爬虫&;起点中文网完本榜500部小说
- gitlab安装随记
- python中pyperclip库的功能
- springboot秒杀课程学习整理1-6
- csla框架__使用Factory方式实现Csla.BusinessBase对象数据处理
- MapReduce实现Apriori算法
- [Linux] Extend space of root disk in Linux7