首页 > 代码库 > 递归求最大公约数
递归求最大公约数
import java.io.IOException;import java.util.Scanner;public class CommonDivisor { public static void main(String[] args)throws IOException{ try{ System.out.println("请输入两个整数!"); Scanner input = new Scanner(System.in); int x = input.nextInt(); int y = input.nextInt(); System.out.println("两个整数的最大公约数为:" + gcd(x,y)); } catch(Exception ex){ System.out.println("需要输入的数为整数!"); } } public static int gcd(int m,int n){ int result = 0; if(m % n ==0){ result = n; } else result = gcd(n,m % n); return result; }}
用递归,求两个整数的最大公约数。
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。