UVA 11181 - Probability|Given (概率DFS)
2013-11-19 20:15
295 查看
Problem G
Probability|Given
Input: Standard Input
Output: Standard Output
N friends go to the local super market together. The probability of their buying something from the market is
respectively. After their marketing is finished you
are given the information that exactly r of them has bought something and others have bought nothing. Given this information you will have to find their individual buying probability.
Input
The input file contains at most 50 sets of inputs. The description of each set is given below:
First line of each set contains two integers N (1 ≤ N ≤ 20) and r(0 ≤ r ≤ N). Meaning of N and r are given in the problem statement. Each of the next N lines contains one floating-point number
(0.1<
<1)
which actually denotes the buying probability of the i-th friend. All probability values should have at most two digits after the decimal point.
Input is terminated by a case where the value of N and r is zero. This case should not be processes.
For each line of input produce N+1 lines of output. First line contains the serial of output. Each of the next N lines contains a floating-point number which denotes the buying probability of the i-th friend given that exactly r has bought something. These
题意:给定n,r表示有n个人,其中r个人买东西,每个人买东西概率为pi,n个人中r个人买东西,输出每个人包含在其中的概率
思路:dfs,每个人都可能买与不买,然后dfs过程记录下来即可
代码:
#include <stdio.h>
#include <string.h>
const int N = 25;
int n, r, i;
double p
, zi
;
void init() {
memset(zi, 0, sizeof(zi));
for (i = 0; i < n; i ++)
scanf("%lf", &p[i]);
}
double dfs(int num, double P, int numr) {
if (num == n) {
if (numr == r)
return P;
return 0;
}
double p1 = 0, p2 = 0, ans = 0;
if (numr < r) {
p1 = dfs(num + 1, P * p[num], numr + 1);
zi[num] += p1;
}
p2 = dfs(num + 1, P * (1 - p[num]), numr);
ans = p1 + p2;
return ans;
}
void solve() {
init();
double mu = dfs(0, 1, 0);
for (int i = 0; i < n; i ++)
printf("%.6lf\n", zi[i] / mu);
}
int main() {
int t =0;
while (~scanf("%d%d", &n, &r) && n + r) {
printf("Case %d:\n", ++t);
solve();
}
return 0;
}
Probability|Given
Input: Standard Input
Output: Standard Output
N friends go to the local super market together. The probability of their buying something from the market is
respectively. After their marketing is finished you
are given the information that exactly r of them has bought something and others have bought nothing. Given this information you will have to find their individual buying probability.
Input
The input file contains at most 50 sets of inputs. The description of each set is given below:
First line of each set contains two integers N (1 ≤ N ≤ 20) and r(0 ≤ r ≤ N). Meaning of N and r are given in the problem statement. Each of the next N lines contains one floating-point number
(0.1<
<1)
which actually denotes the buying probability of the i-th friend. All probability values should have at most two digits after the decimal point.
Input is terminated by a case where the value of N and r is zero. This case should not be processes.
Output
For each line of input produce N+1 lines of output. First line contains the serial of output. Each of the next N lines contains a floating-point number which denotes the buying probability of the i-th friend given that exactly r has bought something. These
values should have six digits after the decimal point. Follow the exact format shown in output for sample input. Small precision errors will be allowed. For reasonable precision level use double precision floating-point numbers.
Sample Input Output for Sample Input
3 2 0.10 0.20 0.30 5 1 0.10 0.10 0.10 0.10 0.10 0 0 | Case 1: 0.413043 0.739130 0.847826 Case 2: 0.200000 0.200000 0.200000 0.200000 0.200000 |
思路:dfs,每个人都可能买与不买,然后dfs过程记录下来即可
代码:
#include <stdio.h>
#include <string.h>
const int N = 25;
int n, r, i;
double p
, zi
;
void init() {
memset(zi, 0, sizeof(zi));
for (i = 0; i < n; i ++)
scanf("%lf", &p[i]);
}
double dfs(int num, double P, int numr) {
if (num == n) {
if (numr == r)
return P;
return 0;
}
double p1 = 0, p2 = 0, ans = 0;
if (numr < r) {
p1 = dfs(num + 1, P * p[num], numr + 1);
zi[num] += p1;
}
p2 = dfs(num + 1, P * (1 - p[num]), numr);
ans = p1 + p2;
return ans;
}
void solve() {
init();
double mu = dfs(0, 1, 0);
for (int i = 0; i < n; i ++)
printf("%.6lf\n", zi[i] / mu);
}
int main() {
int t =0;
while (~scanf("%d%d", &n, &r) && n + r) {
printf("Case %d:\n", ++t);
solve();
}
return 0;
}
相关文章推荐
- UVA 11181(C) ——Probability|Given (条件概率, dfs求组合)
- UVA - 11181 Probability|Given (dfs处理条件概率)
- UVA - 11181 Probability|Given(条件概率+dfs枚举组合)
- uva 11181 - Probability|Given(概率)
- UVA 11181 Probability|Given (离散概率)
- UVA.11181 Probability|Given (概率)
- 【UVA】11181 - Probability|Given(条件概率)
- UVA-11181 Probability|Given(概率)
- UVa 11181 Probability|Given (DFS&贝叶斯公式)
- Uva - 11181 Probability|Given (条件概率)
- uva 11181 - Probability|Given(概率)
- uva 11181 Probability|Given 条件概率
- UVa 11181 - Probability|Given (条件概率)
- UVA - 11181 Probability|Given 概率 + 递归枚举子集
- Probability|Given - UVa 11181 概率dp
- Uva 11181 Probability|Given(概率dp)
- UVA 11181 Probability|Given 概率
- UVa 11181 - Probability|Given 概率
- UVa 11181 (条件概率) Probability|Given
- uva11181 - Probability|Given 条件概率