【JAVA】UVA 10056 - What is the Probability?

題目原文:https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=997

Sample Input:
2
2 0.166666 1
2 0.166666 2

Sample Output:
0.5455
0.4545

題目大意:給你一個骰子,設現在有n人,骰到某數獲勝(1/6),計算出獲勝的機率。

第一行輸入幾組測資
第二行輸入參加遊戲的人數、獲勝的機率、第幾個人獲勝

※設N=人數、P=獲勝機率、I=第幾個人獲勝

那麼每個回合獲勝的機率即為:

import java.util.*;
public class main{
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
		int count = sc.nextInt();

		for (int i = 0; i < count; i++) {
		int N = sc.nextInt();
		double P = sc.nextDouble();
		int I = sc.nextInt();
	
		System.out.printf("%.4f", P == 0 ? P : P * Math.pow(1 - P, I - 1) / (1 - Math.pow(1 - P, N)));
		System.out.println();
		}

		sc.close();
    }
};