PTA L1-013 计算阶乘和 C++实现

计算阶乘和

对于给定的正整数N,需要你计算 S=1!+2!+3!+…+N!。
输入格式:
输入在一行中给出一个不超过10的正整数N。
输出格式:
在一行中输出S的值。
输入样例:

3

输出样例:

9

Talk is cheap. Show me the code.

#include<iostream>

using namespace std;
int main()
{    
	int n;    
	cin>>n;    
	int sum=0;    
	for(int i=1;i<=n;i++)    
	{        
		int temp=1;        
		for(int j=1;j<=i;j++)        
		{            
			temp=temp*j;        
		}        
		sum=sum+temp;    
	}    
	cout<<sum;
} 

解题思路

用两层循环,外层计算求和,内层求阶乘即可。

测试结果

在这里插入图片描述

发布了24 篇原创文章 · 获赞 1 · 访问量 219

猜你喜欢

转载自blog.csdn.net/weixin_43646424/article/details/104432976