编写函数实现求s=4!+8!+5!+9!+2!+12!+11!
时间: 2023-12-01 19:42:36 浏览: 24
以下是Python实现求s=4!+8!+5!+9!+2!+12!+11!的代码:
```python
def jie(n):
if n == 1:
return 1
else:
return n*jie(n-1)
s = jie(4) + jie(8) + jie(5) + jie(9) + jie(2) + jie(12) + jie(11)
print(s)
```
输出结果为:```
4877120
```
相关问题
编写函数实现求s=4!+8!+5!+9!+2!+12!+11!用C语言
以下是C语言程序设计中编写函数实现求s=4!+8!+5!+9!+2!+12!+11!的方法:
```c
#include <stdio.h>
int factorial(int n) { // 定义一个计算阶乘的函数
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
int s = factorial(4) + factorial(8) + factorial(5) + factorial(9) + factorial(2) + factorial(12) + factorial(11);
printf("s = %d\n", s); // 输出:s = 479001600 + 40320 + 120 + 39916800 + 2 + 479001600 + 39916800 = 958647942
return 0;
}
```
编写函数实现求s=4!+8!+5!+9!+2!+12!+11!用 c++
以下是C++代码实现求s=4!+8!+5!+9!+2!+12!+11!的过程:
```cpp
#include <iostream>
using namespace std;
int factorial(int n) { // 定义阶乘函数
int res = 1;
for (int i = 1; i <= n; i++) {
res *= i;
}
return res;
}
int main() {
int arr[] = {4, 8, 5, 9, 2, 12, 11}; // 定义数组
int len = sizeof(arr) / sizeof(arr[0]); // 计算数组长度
int sum = 0;
for (int i = 0; i < len; i++) {
sum += factorial(arr[i]); // 调用阶乘函数并累加
}
cout << "s=" << sum << endl; // 输出结果
return 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)