日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

poj 3101 Astronomy (java 分数的最小公倍数 gcd)

發(fā)布時(shí)間:2025/3/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 poj 3101 Astronomy (java 分数的最小公倍数 gcd) 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接

要用大數(shù),看了別人的博客,用java寫的。

題意:求n個(gè)運(yùn)動(dòng)周期不完全相同的天體在一條直線上的周期。

分析:兩個(gè)星球周期為a,b。則相差半周的長(zhǎng)度為a*b/(2*abs(a-b)),對(duì)于n個(gè)只需求這n個(gè)

分?jǐn)?shù)的最小公倍數(shù)即可。 分?jǐn)?shù)的最小公倍數(shù) = 分子的最小公倍數(shù)/分母的最大公約數(shù) 1 import java.util.*; 2 import java.math.*; 3 public class Main { 4 public static int [] t = new int [1200]; 5 public static int [] tt = new int [1200]; 6 public static BigInteger [] fz = new BigInteger [1200]; 7 public static BigInteger [] fm = new BigInteger [1200]; 8 9 public static int gcd(int a, int b){ 10 return b==0?a:gcd(b, a%b); 11 } 12 public static void main(String args[]){ 13 int n, m; 14 Scanner cin=new Scanner(System.in); 15 n = cin.nextInt(); 16 for(int i = 0; i < n; i++) 17 t[i] = cin.nextInt(); 18 Arrays.sort(t, 0, n); //對(duì)數(shù)組排序 19 m = 0; 20 tt[m++] = t[0]; 21 for(int i = 1; i < n; i++) 22 if(t[i]!=t[i-1]) 23 tt[m++] = t[i]; 24 for(int i = 1; i < m; i++){ 25 int a = tt[i]*t[0]; 26 int b = 2*(tt[i]-tt[0]); 27 int g = gcd(a, b); 28 fz[i] = BigInteger.valueOf(a/g); 29 fm[i] = BigInteger.valueOf(b/g); 30 } 31 BigInteger t1, t2; 32 t1 = fz[1]; t2 = fm[1]; 33 for(int i = 2; i < m; i++) 34 { 35 BigInteger aa = t1.multiply(fz[i]); 36 BigInteger gg = t1.gcd(fz[i]); 37 t1 = aa.divide(gg); 38 t2 = t2.gcd(fm[i]); 39 } 40 System.out.println(t1+" "+t2); 41 } 42 }

轉(zhuǎn)載于:https://www.cnblogs.com/bfshm/p/3860044.html

總結(jié)

以上是生活随笔為你收集整理的poj 3101 Astronomy (java 分数的最小公倍数 gcd)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。