《内蒙古自治区第十二届大学生程序设计竞赛试题_D: 正品的概率》

问题 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 }



原文地址:https://www.cnblogs.com/kangxinxin/p/10794224.html