《內蒙古自治區第十二屆大學生程序設計競賽試題_D: 正品的概率》
阿新 • • 發佈:2019-04-30
思路 ret positive page string n) 分享 ive all
輸入樣例 復制
問題 D: 正品的概率
內存限制:128 MB時間限制:1 S標準輸入輸出 題目類型:傳統評測方式:文本比較上傳者:外部導入 提交:36通過:7 返回比賽提交提交記錄題目描述
袋中有m枚正品硬幣,n枚次品硬幣(次品硬幣兩面都有國徽),在袋中任取一枚,將它投擲k次,已知每次得到的都是國徽,那麽這枚硬幣是正品的概率是多少?輸入格式
輸入包含多組數據,每組占一行包含3個正整m,n,k (1<=m,n,k<=50)。輸出格式
每組輸出一行,包含一個最簡分數,硬幣為正品的概率。輸入樣例 復制
1 1 1
輸出樣例 復制
1/3
解題思路,一個是 貝葉斯公式和 最大公約數的問題
(圖是盜的:原連接_ https://blog.csdn.net/hy971216/article/details/78633375 )
Java 代碼實現
1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main { 5 6 public static BigInteger getPow(BigInteger a,BigInteger n){7 BigInteger res = new BigInteger("1"); 8 for(int i=1;i<=n.intValue();i++){ 9 res = res.multiply(a); 10 } 11 return res; 12 } 13 14 public static void main(String[] args) { 15 16 Scanner cin = new Scanner(System.in);17 while(cin.hasNext()){ 18 BigInteger m = new BigInteger(cin.next()); 19 BigInteger n = new BigInteger(cin.next()); 20 BigInteger k = new BigInteger(cin.next()); 21 22 BigInteger res = m.add(n.multiply(getPow(new BigInteger("2"), k))); 23 BigInteger gcd = res.gcd(m); 24 System.out.println(m.divide(gcd)+"/"+res.divide(gcd)); 25 } 26 27 } 28 29 }
《內蒙古自治區第十二屆大學生程序設計競賽試題_D: 正品的概率》