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));
    
    	}
    
    }


  • 相关阅读:
    razor 拼接字符串
    转,CV和resume的区别
    b/s开发者的困境
    sql 下,float和numeric
    VS2010 + Entity FrameWork 4.4 +Mvc 4.0 出现的错误
    Nuget 管理entity framework
    关于 Code First
    C# 输出控制台结果到文件
    WCF wsdlexception(at/html):faultCode=INVALID_WSDL
    Silverlight环境配置
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/7070350.html
Copyright © 2011-2022 走看看