1131: 零起点学算法38——求阶乘和

缺乏、安全感 2023-01-10 13:23 296阅读 0赞

Description

输入一个正整数n(n<=10),计算
S=1!+2!+3!+…+n!

Input

输入一个正整数n(n<=10)(多组数据)

Output

输出S(每组数据一行)

" class="reference-link">Sample Input 5f8f312f51791b8bb0ed0ae07c2ffa43.gif

  1. 2

Sample Output

  1. 3

Source

零起点学算法

Code

  1. #include<iostream>
  2. #include<stdio.h>
  3. #include<iomanip>
  4. #include<math.h>
  5. using namespace std;
  6. int main()
  7. {
  8. int n;
  9. while(~scanf("%d",&n))
  10. {
  11. int ans2=0;
  12. for(int i=1;i<=n;i++)
  13. {
  14. int ans=1;
  15. for(int j=1;j<=i;j++)
  16. ans*=j;
  17. ans2+=ans;
  18. }
  19. cout<<ans2<<endl;
  20. }
  21. }

发表评论

表情:
评论列表 (有 0 条评论,296人围观)

还没有评论,来说两句吧...

相关阅读