初学求助!
输入n,求n!+(n-1)!+.......2!+1!(不知道哪儿错了,求指教)#include <iostream.h>
void main()
{
int n,i=1,j=2,s=1;
cin >> n;
if (n<=1)
cout <<"阶层和为:1"<<endl;
else
{
while(n>=2)
{
while(n>=j)
{
i=i*j;
j++;
}
n--;
s=s+i;
}
cout<<"阶层和为:"<<s<<endl;
}
}
#include <iostream>
using namespace std;
void main()
{
int n,fac = 1, facSum = 0, temp;
cin >> n;
while(n)
{
temp = n;
fac = 1; //重新初始化 fac
while(temp)
{
fac *= temp;
temp--;
}
cout << n << " 的阶乘为:" << fac <<endl;
facSum += fac;
n--;
}
cout << "阶乘和为:" << facSum <<endl;
}
页:
[1]