求两个数的最大公因数
2015-08-19 17:52
323 查看
/* Enter two integer: 33 100 They haven't any common divizor. Enter two integer: 100 330 This greatest common divisor for 100 and 330 is 10 Enter two integer: 222 123 This greatest common divisor for 222 and 123 is 3 */ import java.util.Scanner; public class GreatestCommonDivisorMethod { public static void main(String[] args) { Scanner input = new Scanner(System.in); for(;;) { System.out.print("Enter two integer: "); int n1 = input.nextInt(); int n2 = input.nextInt(); int greatestCommonDivisor = Gcd(n1, n2); if(greatestCommonDivisor == 0) System.out.println("They haven't any common divizor."); else System.out.println("This greatest common divisor for " + n1 + " and " + n2 + " is " + greatestCommonDivisor); } } public static int Gcd(int n1, int n2) { int gcd = 0; int k = 2; while (n1 >= k && n2 >= k) { if (n1 % k == 0 && n2 % k == 0) gcd = k; k++; } return gcd; } }
相关文章推荐
- php的html_entity_decode函数
- gerrit编译插件方法
- 黑马程序员-----基本语法
- Spring aop实现方式记录
- 数据库开发基础
- Android性能优化典范-第3季
- Mybatis-There is no getter for property named 'tj' in 'class java.lang.String'
- Linux下限制进程的CPU利用率
- Linux编程——gdb调试
- LA 3027 Corporative Network(并查集,求某个节点到根节点的距离)
- HDOJ 1874 畅通工程续 (最短路之SPFA)
- zeroclipboard兼容多浏览器复制到粘贴板功能
- MFC CString的使用
- 使用FileItem获取文件名时注意事项
- 从数据库导出数据时,有的字段是时间,不同的时间向在窗口中去掉时用正则表达式匹配找到不同的时间
- H5上传图片
- [Java] Applet
- 多核CPU怎么理解
- dir-takeown-icacls-rd命令的用法
- 直接插入排序