Java中使用辗转相除法求最大公约数
更新时间:2015年05月20日 10:34:03 投稿:junjie
这篇文章主要介绍了Java中使用辗转相除法求最大公约数,本文直接给出代码实例,需要的朋友可以参考下
比较好用的是辗转相除法。
比如:49和91
a b temp
49 % 91 = 49
91 % 49 = 42
49 % 42 = 7
42 % 7 = 0
所以最大公约数就是7.
public class T {
public static void main(String[] args) {
int gcd = gcd(91, 49);
System.out.println(gcd);
}
/**
* greatest commond divisor
* @param a
* @param b
* @return
*/
public static int gcd(int a, int b) {
while(b != 0) {
int temp = a%b;
a = b;
b = temp;
}
return a;
}
}
相关文章
SpringBoot中@EnableAutoConfiguration注解的实现
Spring Boot@EnableAutoConfiguration是一个强大的工具,可以简化配置过程,从而实现快速开发,本文主要介绍了SpringBoot中@EnableAutoConfiguration注解的实现,感兴趣的可以了解一下2024-01-01
SpringBoot整合Swagger和Actuator的使用教程详解
Swagger 是一套基于 OpenAPI 规范构建的开源工具,可以帮助我们设计、构建、记录以及使用 Rest API。本篇文章主要介绍的是SpringBoot整合Swagger(API文档生成框架)和SpringBoot整合Actuator(项目监控)使用教程。感兴趣的朋友一起看看吧2019-06-06


最新评论