zoukankan      html  css  js  c++  java
  • 上机题目(0基础)-计算两个正整数的最大公约数和最小公倍数(Java)

    题目例如以下:


    代码例如以下:

    package huawei;
    
    import java.util.Scanner;
    
    public final class Demo {
    
    	// 功能:获取两个整数的最大公约数
    	// 输入:两个整数
    	// 返回:最大公约数
    	public static long getMaxDivisor(long lFirstInput, long lSecondInput) {
    		while (lSecondInput % lFirstInput != 0) {
    			/**
    			 * 运用递归调用求余值作min 前min作max直求余值0止结束循环
    			 */
    			int temp = (int) (lSecondInput % lFirstInput);
    			lSecondInput = lFirstInput;
    			lFirstInput = temp;
    		}
    		return lFirstInput;
    	}
    
    	// 功能:获取两个整数的最小公倍数
    	// 输入:两个整数
    	// 返回:最小公倍数
    	public static long getMinMultiple(long lFirstInput, long lSecondInput) {
    
    		return lFirstInput * lSecondInput / getMaxDivisor(lFirstInput, lSecondInput);
    	}
    
    	public static void main(String args[]) {
    		int first, second;
    		Scanner cin = new Scanner(System.in);
    		System.out.println("int first:");
    		first = cin.nextInt();
    		System.out.println("int second:");
    		second = cin.nextInt();
    
    		System.out.println(getMaxDivisor(first, second));
    		System.out.println(getMinMultiple(first, second));
    
    	}
    
    }


  • 相关阅读:
    solr集群
    mybatis的逆向工程
    使用secureCRT上传下载
    Linux vim基本的使用方法
    非web下的PowerMockito单元测试
    打印日志
    集群
    免安装版tomcat安装成服务
    apache安装zip包安装(非exe)
    Java 性能优化(一)
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7070350.html
Copyright © 2011-2022 走看看