C - u Calculate e

   A simple mathematical formula for e is



where n is allowed to go to infinity. This can actually yield very accurate approximations of e using relatively small values of n.
Output Output the approximations of e generated by the above formula for the values of n from 0 to 9. The beginning of your output should appear similar to that shown below.
Sample Output
n e
- -----------
0 1
1 2
2 2.5
3 2.666666667

4 2.708333333

#include<iostream>

#include<stdlib.h>

#include<cmath>

#include<cstring>

using namespace std;

long long mi(int l){

if(l==0) 

 return 1;

long long num=1;

for(int i=1;i<=l;i++)

{

num=num*i;

}

return num;

}

int main(){

double e=0

;cout<<"n e"<<endl;

cout<<"- -----------"<<endl;

for(int i=0;i<=2;i++) {

e=e+1.0/mi(i);

cout<<i<<" "<<e<<endl; 

}

for(int i=3;i<=9;i++)

{

e=e+1.0/mi(i);

printf("%d %.9lf\n",i,e); 

}

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这个问题涉及到多个约束条件和变量,需要使用复杂的优化算法进行求解,遗传算法是其中一种可行的方法。下面是大致的算法流程和MATLAB代码实现: 算法流程: 1. 初始化种群,即生成初始的随机解(TC和Anl的取值)。 2. 计算每个个体的适应度函数值,即根据TC和Anl计算配送次数并加入目标函数中,同时考虑所有的约束条件。 3. 选择操作,即根据适应度函数值选择一部分个体作为下一代的父代。 4. 交叉操作,即将父代个体进行交叉操作生成子代个体。 5. 变异操作,即对子代个体进行变异操作,引入新的随机变量。 6. 重复2-5步直到满足停止条件,如达到最大迭代次数或者找到满足要求的最优解。 MATLAB代码实现: 由于问题涉及到多个变量和约束条件,代码实现比较复杂,这里仅提供一个大致的框架供参考: ``` function [TC, Anl] = genetic_algorithm() % 设置算法参数 population_size = 100; % 种群大小 max_generation = 100; % 最大迭代次数 crossover_rate = 0.8; % 交叉概率 mutation_rate = 0.01; % 变异概率 L_max = 10; % 车辆能装的最大数量 % 初始化种群 population = initialize_population(population_size); % 迭代求解 for generation = 1:max_generation % 计算适应度函数值 fitness = calculate_fitness(population); % 选择操作 parents = selection(population, fitness); % 交叉操作 offspring = crossover(parents, crossover_rate); % 变异操作 offspring = mutation(offspring, mutation_rate); % 合并父代和子代 population = [population; offspring]; % 保留种群前population_size个个体 population = population(1:population_size, :); % 输出当前迭代的最优解 [best_fitness, best_individual] = max(fitness); fprintf('Generation %d, Best Fitness: %f\n', generation, best_fitness); % 满足停止条件则结束 if best_fitness == 0 break; end end % 输出最终的最优解 [best_fitness, best_individual] = max(fitness); TC = population(best_individual, 1:6); Anl = population(best_individual, 7:end); end % 初始化种群 function population = initialize_population(population_size) % 生成随机的TC和Anl取值 % ... end % 计算适应度函数值 function fitness = calculate_fitness(population) % 根据TC和Anl计算配送次数并加入目标函数中,同时考虑所有的约束条件 % ... end % 选择操作 function parents = selection(population, fitness) % 根据适应度函数值选择一部分个体作为下一代的父代 % ... end % 交叉操作 function offspring = crossover(parents, crossover_rate) % 将父代个体进行交叉操作生成子代个体 % ... end % 变异操作 function offspring = mutation(offspring, mutation_rate) % 对子代个体进行变异操作,引入新的随机变量 % ... end ``` 以上代码仅为框架,具体实现中需要根据问题的具体情况进行更改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值