C++ 阶乘求和 - 算法练习题

本题要求你编写一个 C++ 程序,计算不大于给定正整数 n 的所有正整数阶乘的和。输入一个正整数 n(1 < n < 12),程序输出阶乘的和。

输入描述

输入有一行,包含一个正整数 n(1 < n < 12)。

输出描述

输出有一行:阶乘的和。

示例

用例输入 1

3

用例输出 1

9

程序内容

#include <iostream>
using namespace std;

int main() { int n; cin >> n;

int factorial = 1;
int sum = 0;

for (int i = 1; i &lt;= n; i++) {
    factorial *= i;
    sum += factorial;
}

cout &lt;&lt; sum &lt;&lt; endl;

return 0;

}'</pr


原文地址: https://www.cveoy.top/t/topic/qiMY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录