java语言程序设计基础篇第6章6.29求最小公倍数

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

importjavax.swing.*;importjava.math.*;publicclassLCM{staticfinalintNUM=100;staticint[]prime_array=newint[NUM];staticvoidcreatePrimeArray(){intn=2;intk=0;while(k100){if(isPrime(n)){prime_array[k]=n;k++;}n++;}}staticbooleanisPrime(intn){if(n2)returnfalse;for(inti=2;in;i++)if(n%i==0)returnfalse;returntrue;}publicstaticint[]getPrimeFactors(intm){System.out.println(========getPrimeFactors============);System.out.println(m);int[]factors=newint[NUM];while(m1)for(inti=0;iNUM;i++){if(m%prime_array[i]==0){factors[i]+=1;m=m/prime_array[i];}}for(inti=0;iNUM;i++){if(factors[i]!=0)System.out.println(prime_array[i]+\t+factors[i]);}returnfactors;}staticintpow(intx,intz){intp=1;for(inti=0;iz;i++)p*=x;returnp;}staticintgetLCM(int[]factors1,int[]factors2){System.out.println(========getLCM============);intlcm=1;for(inti=0;iNUM;i++){intmax=(factors2[i]factors1[i])?factors2[i]:factors1[i];if(max=1){lcm*=pow(prime_array[i],max);System.out.println(prime_array[i]+\t+max);}}returnlcm;}publicstaticvoidmain(String[]args){createPrimeArray();Strings1=JOptionPane.showInputDialog(null,Thefirstinteger);Strings2=JOptionPane.showInputDialog(null,Thesecondinteger);intk1=Integer.parseInt(s1);intk2=Integer.parseInt(s2);int[]factors1=getPrimeFactors(k1);int[]factors2=getPrimeFactors(k2);intlcm=getLCM(factors1,factors2);System.out.println(lcm);}}

1 / 4
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功