【UVA】11181 - Probability|Given(条件概率)

本文介绍了一个具体的条件概率问题解决方法,通过递归枚举的方式计算给定条件下每个人购买物品的概率,并给出了完整的C++代码实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一道条件概率题,数学烂真的伤不起,一开始都不知道怎么求条件概率。

P(e) = p(e|E)/p(E).

用e出现的情况的概率,除以所有情况出现的概率,递归枚举每个人是否买东西了。

14026058 11181 Probability|Given Accepted C++ 0.102 2014-08-12 08:25:51

效率可能有点差。

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<list>
#include<cmath>
#include<string>
#include<sstream>
#include<ctime>
using namespace std;
#define _PI acos(-1.0)
#define INF (1 << 10)
#define esp 1e-6
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> pill;
/*===========================================
===========================================*/
#define MAXD 20 + 5
int n,m;
double p[MAXD];
double Sum ;
double _sum[MAXD];
int vis[MAXD];
bool Input(){
    scanf("%d%d",&n,&m);
    if(!n && !m)
        return false;
    for(int i = 0 ; i < n ; i++)
        scanf("%lf",&p[i]);
    return true;
}
void dfs(int cur, int key ,int now,double value){
    if(key == 1){  /*这个人买东西了*/
        value = value * p[cur];
        vis[cur] = 1;  /*表示这个人买东西了*/
    }
    else{
        value = value * (1 - p[cur]);
        vis[cur] = 0;
    }
    if(cur == n - 1){
        if(now == m){
        Sum += value;
        for(int i = 0 ; i < n ; i++)
            if(vis[i])
                _sum[i] += value;
        }
        return ;
    }
    if(now < m)
    dfs(cur + 1, 1 , now + 1 , value); /*假设下一个人买东西了*/
    dfs(cur + 1, 0 , now , value);     /*假设下一个人没买东西*/
    return ;
}
int main(){
    int Case = 1;
    while(Input()){
        Sum = 0;
        memset(_sum,0,sizeof(_sum));
        memset(vis,0,sizeof(vis));
        dfs(0,0,0,1);
        dfs(0,1,1,1);
        printf("Case %d:\n",Case ++);
        for(int i = 0 ; i < n ; i++){
            double ans  =  _sum[i] / Sum;
            printf("%.6f\n",ans);
        }
    }
    return 0;
}

要使用遗传算法求解函数的最小值,可以按照以下步骤进行编写代码: 1. 定义适应度函数:根据给定的函数表达式,计算适应度函数的值。适应度函数的值越小,表示个体的适应度越高。 ```python def fitness_function(x): x1, x2, x3, x4, x5, x6, x7 = x y = 100 / (abs(x1 + 1) + abs(x2) + abs(x3 - 1) + abs(x4 + 2) + abs(x5 + 3) + abs(x6 - 2) + abs(x7 - 3) + 1) return y ``` 2. 初始化种群:随机生成一组个体作为初始种群。 ```python import random def initialize_population(population_size, chromosome_length): population = [] for _ in range(population_size): individual = [random.uniform(-10, 10) for _ in range(chromosome_length)] population.append(individual) return population ``` 3. 选择操作:使用轮盘赌选择算法,根据个体的适应度选择一部分个体作为下一代的父代。 ```python def selection(population, fitness_values): total_fitness = sum(fitness_values) probabilities = [fitness / total_fitness for fitness in fitness_values] selected_population = [] for _ in range(len(population)): r = random.random() cumulative_probability = 0 for i, probability in enumerate(probabilities): cumulative_probability += probability if r <= cumulative_probability: selected_population.append(population[i]) break return selected_population ``` 4. 交叉操作:使用单点交叉算法,对父代个体进行交叉操作生成子代个体。 ```python def crossover(parent1, parent2): crossover_point = random.randint(1, len(parent1) - 1) child1 = parent1[:crossover_point] + parent2[crossover_point:] child2 = parent2[:crossover_point] + parent1[crossover_point:] return child1, child2 ``` 5. 变异操作:对子代个体进行变异操作,增加种群的多样性。 ```python def mutation(individual, mutation_rate): mutated_individual = individual.copy() for i in range(len(mutated_individual)): if random.random() < mutation_rate: mutated_individual[i] = random.uniform(-10, 10) return mutated_individual ``` 6. 遗传算法主程序:根据上述操作,进行遗传算法的迭代过程。 ```python def genetic_algorithm(population_size, chromosome_length, generations, mutation_rate): population = initialize_population(population_size, chromosome_length) for _ in range(generations): fitness_values = [fitness_function(individual) for individual in population] selected_population = selection(population, fitness_values) new_population = [] while len(new_population) < population_size: parent1 = random.choice(selected_population) parent2 = random.choice(selected_population) child1, child2 = crossover(parent1, parent2) mutated_child1 = mutation(child1, mutation_rate) mutated_child2 = mutation(child2, mutation_rate) new_population.extend([mutated_child1, mutated_child2]) population = new_population best_individual = max(population, key=fitness_function) best_fitness = fitness_function(best_individual) return best_individual, best_fitness ``` 7. 调用遗传算法函数并输出结果。 ```python population_size = 100 chromosome_length = 7 generations = 100 mutation_rate = 0.01 best_individual, best_fitness = genetic_algorithm(population_size, chromosome_length, generations, mutation_rate) print("最优个体:", best_individual) print("最优适应度:", best_fitness) ``` 这样就可以使用遗传算法求解给定函数的最小值了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值