importjava.util.Scanner;publicclassMain{publicstaticvoidmain(String[] args){Scanner input =newScanner(System.in);int n = input.nextInt();int ans =1;// 阶乘的和,初始为第1项int f =1;// 当前项的阶乘,初始为第1项/* 从第二项开始,到最后一项为止 */for(int i =2; i <= n; i++){
f = f * i;// 首先,计算当前项的阶乘
ans = ans + f;// 其次,累加当前项的阶乘}System.out.print(ans);}}
四、C++程序
#include<iostream>usingnamespace std;intmain(){int n;
cin >> n;int ans =1;// 阶乘的和,初始为第1项int f =1;// 当前项的阶乘,初始为第1项/* 从第二项开始,到最后一项为止 */for(int i =2; i <= n; i++){
f = f * i;// 首先,计算当前项的阶乘
ans = ans + f;// 其次,累加当前项的阶乘}
cout << ans;return0;}