求取一组整数的最小公倍数算法

来源:互联网 发布:mac os shimo 破解版 编辑:程序博客网 时间:2024/06/01 09:28
package work;import java.util.InputMismatchException;import java.util.Random;import java.util.Scanner;import com.sun.javafx.scene.traversal.Algorithm;/**此类用于求取一组int型数组中所有元素的最小公倍数 * @time  2017年3月17日星期五 *  */public class work {int n;//数组长度int[] arr;//数组/**初始化数组长度n与数组arr * @exception InputMismatchException 输入的数据是否匹配,否则抛出异常,并返回false,是则返回true * @return 返回值为boolean,判断数组是否正确建立 */public boolean init(){try{Scanner in=new Scanner(System.in);System.out.print("请输入数组长度:");n=in.nextInt();//输入数组长度arr=new int[n];System.out.println("请输入数组元素(以空格分开):");for(int i=0;i<n;i++){//向数组中输入数据arr[i]=in.nextInt();}}catch(InputMismatchException e){//处理异常e.printStackTrace();System.out.println("输入错误,请重新输入");return false;}return true;}/**计算两个整数的最大公约数 * @return 返回两个参数的最大公约数 */public int algorthm(int max,int min){int x;int temp;if (max < min){temp = max;max = min;min = temp;}if ((x=max%min) !=  0)//当余数为0时,得到最大公约数{return algorthm(min, x);//余数不为0时,递归调用,反复计算直到余数为0结束}else{return min;}}/** 此函数使用最小公倍数公式求得最小公倍数 * @return 返回存储在数组最后一个元素中的最终运算结果 * @throws StackOverflowError 若得到数值大于int值范围,则抛出错误 * @see  */public  int result1()throws StackOverflowError{//若得到数值大于int范围,则抛出错误try{for(int i=0;i<n-1;i++){  int x=arr[i]*arr[i+1]/algorthm(arr[i], arr[i+1]);//求最小公倍数公式,两元数之积等于最小公倍数与最大公约数之积arr[i]=0;arr[i+1]=x;}}catch( StackOverflowError e){System.out.println("数组中元素过多或太大,超出范围");throw new StackOverflowError("数组中元素过多或太大,超出范围");}return arr[n-1];}/**此函数使用短除法求得最小公倍数 * @return 返回存储在最后一个元素中的最终运算结果 * @throws StackOverflowError 若得到数值大于int范围,则抛出错误. */public int result2()throws StackOverflowError{//若得到数值大于int范围,则抛出错误try{int m;int x;for(int i=0;i<n-1;i++){m=1;x=1;while(algorthm(arr[i], arr[i+1])!=1){//用短除法if(arr[i]%m==0&&arr[i+1]%m==0){x=x*m;arr[i]=arr[i]/m;arr[i+1]=arr[i+1]/m;m++;}elsem++;}arr[i+1]=x*arr[i+1]*arr[i];}}catch( StackOverflowError e){System.out.println("数组中元素过多或太大,超出范围");throw new StackOverflowError("数组中元素过多或太大,超出范围");}return arr[n-1];}/** * 应用程序界面 */public static void maininterface(){System.out.println("****************************************");System.out.println("****************************************");System.out.println("********       1、算法1         ********");System.out.println("********       2、算法2         ********");System.out.println("********       3、退出                            ********");System.out.println("****************************************");System.out.println("****************************************");}public static void main(String[] args) {// TODO Auto-generated method stubwork w=new work();int choose;while(true){maininterface();System.out.println("请输入选择(1~3):");try{//若输入其他类型值,则抛出异常Scanner in=new Scanner(System.in);choose=in.nextInt();switch(choose){//算法选择case 1:w.init();System.out.println(w.result1());break;case 2:w.init();System.out.println(w.result2());break;case 3:System.out.println("程序结束");return;default:System.out.println("输入错误");}}catch(InputMismatchException e){System.out.println("输入格式错误,应输入整形");e.printStackTrace();}}}}

0 0