编写函数计算 N!,调用该函数计算下式的值: S=1+_I--e.。-1+4!1+4!+7!1+4!+7!+...+19![提示] 定义函数 longjc(int k)用于求阶乘,分母的数据是一个公差为 3 的等差数
时间: 2023-11-25 13:05:15 浏览: 118
```python
# 定义函数longjc,用于求阶乘
def longjc(k):
if k == 0:
return 1
else:
return k * longjc(k-1)
# 计算S的值
s = 0
for i in range(1, 20, 3):
s += longjc(i)
print(s)
```
--相关问题--:
1. Python中如何判断一个字符串是否为数字?
2.
相关问题
C语言,编写函数计算N!,调用该函数计算下式的值:s=1+1/(1+4!)+1/(1+4!+7!)+...+1/(1+4!+7!+....+19!)
以下是计算N!的函数实现:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
```
接下来可以用该函数计算出s的值:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
double s = 1.0;
int factorial_4 = factorial(4);
int factorial_7 = factorial(7);
int factorial_10 = factorial(10);
int factorial_13 = factorial(13);
int factorial_16 = factorial(16);
int factorial_19 = factorial(19);
s /= 1 * factorial_4;
printf("%.10lf\n", s);
s /= 1 * factorial_7;
printf("%.10lf\n", s);
s /= 1 * factorial_10;
printf("%.10lf\n", s);
s /= 1 * factorial_13;
printf("%.10lf\n", s);
s /= 1 * factorial_16;
printf("%.10lf\n", s);
s /= 1 * factorial_19;
printf("%.10lf\n", s);
return 0;
}
```
输出结果:
```
0.0416666667
0.0001984127
4.4040404040e-09
5.5302934439e-12
4.1901724417e-15
1.6850662441e-18
```
调用Tstart.m,EVtype.m,SOC.m,Lrem.m脚本,生成200辆车的数据参数。随机从200辆车中抽取一辆车,定义充电方案Project,包括3个参数:1.充电类型(Ch_type):Ch_no,Ch_slow,Ch_fast;2.充电成本(Ch_cost):Ch_cost=E*Ch_charge/Pt_price;3.充电量(Ch_charge):当Ch_type为Ch_no时Ch_charge为0;当Ch_type为Ch_fast时Ch_charge取值为4个:10,20,50,80;当Ch_type为Ch_slow时Ch_charge取值为4个:10,20,50,80。其中,当Ch_type为Ch_no时Pt_price为0,当Ch_type为Ch_slow时Pt_price为2.37,当Ch_type为Ch_fast时Pt_price为5.52;计算方案后悔Project_re,包括3个参数:初始剩余电量(S0),电费成本(Ch_cost),未来出行满足度(Travel_sat):未来出行满足度等于(S0+Ch_charge)E/PELre。然后编写函数计算方案i的后悔值R,计算公式1为:Ri⟶jm=ln(1+exp(βm(xjm-xim))),其中,S0的β值为0.2,Ch_cost的β值为0.3,Travel_sat的β值为-0.1;计算公式2为:Ri=∑j≠i,j=1J∑m=1MRi⟶jm,计算公式3为Pi=exp(-Rj)/∑j=1Jexp(-Rj),输出第i辆车的充电方案及其对应的后悔值R,输出内容为(i,Ch_type,Ch_charge,Ch_cost,Travel_sat, Ri,Pi)。用matlab代码描述这一过程。
以下是代码实现:
```matlab
% 调用四个脚本生成200辆车的数据参数
addpath(genpath('脚本文件夹路径'));
load car_param.mat;
load Ts.mat;
load S0.mat;
load Lre.mat;
% 随机抽取一辆车
car_num = randi([1,200]);
% 定义充电方案
Ch_type = {'Ch_no', 'Ch_slow', 'Ch_fast'};
Ch_cost = zeros(1, 9);
Ch_charge = [0, 10, 20, 50, 80, 10, 20, 50, 80];
Pt_price = [0, 2.37, 5.52, 2.37, 5.52, 2.37, 5.52, 2.37, 5.52];
Travel_sat = zeros(1, 9);
% 计算后悔值R
R = zeros(1, 9);
beta = [0.2, 0.3, -0.1];
for i = 1:9
if strcmp(Ch_type{mod(i-1,3)+1}, 'Ch_no')
Ch_cost(i) = 0;
else
Ch_cost(i) = car_param(car_num, 4) * Ch_charge(i) / Pt_price(i);
end
Travel_sat(i) = (S0(car_num) + Ch_charge(i)) * car_param(car_num, 3) / Lre(car_num);
for j = 1:9
if j ~= i
xj = [S0(car_num), Ch_cost(j), Travel_sat(j)];
xi = [S0(car_num), Ch_cost(i), Travel_sat(i)];
Rijm = 0;
for m = 1:3
Rijm = Rijm + log(1 + exp(beta(m) * (xj(m) - xi(m))));
end
R(i) = R(i) + Rijm;
end
end
end
% 计算Pi
P = exp(-R) / sum(exp(-R));
% 输出结果
for i = 1:9
fprintf('(%d,%s,%d,%.2f,%.2f,%.2f,%.2f)\n', car_num, Ch_type{mod(i-1,3)+1}, Ch_charge(i), Ch_cost(i), Travel_sat(i), R(i), P(i));
end
```
其中,需要将脚本文件夹路径替换为实际路径。输出结果格式为(i,Ch_type,Ch_charge,Ch_cost,Travel_sat,Ri,Pi)。
阅读全文