Skip to content

L1-013 计算阶乘和 ​

对于给定的正整数N,需要你计算 S=1!+2!+3!+...+N!。

输入格式: ​

输入在一行中给出一个不超过 10 的正整数N。

输出格式: ​

在一行中输出S的值。

输入样例: ​

tex
3
3

输出样例: ​

tex
9
9

Solution: ​

java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        int temp = 1, sum = 0;
        for (int i = 1; i <= n; i++) {
            temp *= i;
            sum += temp;
        }
        System.out.println(sum);
    }
}
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(in.readLine());
        int temp = 1, sum = 0;
        for (int i = 1; i <= n; i++) {
            temp *= i;
            sum += temp;
        }
        System.out.println(sum);
    }
}

Released under the MIT License.