###### tags: `CPE-數學計算` # *What is the Probability?* ## 題目: ![](https://i.imgur.com/t9z8RV3.png) >註:本題所需公式:q^(i-1) * p / (1- q^n) >註:setprecision():設定十進位浮點數的精確度(設定後持續有效)。可搭配 fixed 來鎖定小數點後的位數 >註:setw():設定列印時的欄位寬度,若為浮點數,小數點也佔一位。(設定後單次有效) >setfill():指定列印的欄位寬度後,設定左方不足位數要填補的字元符號,預設為空白字元 ``` #include <bits/stdc++.h> using namespace std; int main(){ int s, n, i_th; double p; // probability of successful event double q, ans; cin >> s; for (int i = 0; i < s; i++){ cin >> n >> p >> i_th; if (p == 0){ ans = 0; } else { q = 1 - p; ans = pow(q, i_th-1) * p / (1 - pow(q, n)); } cout << fixed << setprecision(4) << ans << endl; } return 0; } ```