phfb.net
当前位置:首页 >> ptA求阶乘序列前n项和 >>

ptA求阶乘序列前n项和

C++刚刚学到for嵌套循环,老师要写一个N项阶乘的和(1!+2!+3!+4!+n!),一下搞晕了.下面是我写的,给我看一下是错在哪.多多指教,谢谢了#includeusing namespace std;int main (){ int n,i,j,k,count=0; cin >> n; for(i=1;i<=n;i++) { for(j=1,k=1;j<=i;j++) k*=i; count+=k; } cout << count;return 0;}仅供参考!

因为你只要输出结果后六位,所以是不会溢出的,应该是你的算法有问题,比如直接计算n!那么肯定n很大时会溢出.但这一题不需要去直接计算,只要保留后六位就可以.非递归算法:int main() { int p = 1, sum = 0; int i, n; scanf("%d", &n); for(i=1; i<=n; ++i) { p = p*i%1000000; sum = (sum+p)%1000000; } printf("%d", sum); }

#include<iostream>using namespace std;double fac(const int &a){ if (a>1) return fac(a-1)*a; else return 1;}double plu(const int &b){ if (b > 1) return plu(b - 1) + b; else return 1;}int main(){ cout << "input value of n:"; int n = 0; cin >> n; double sum1=

首先是前n项和的代码:#include<stdio.h> main() { int n,i,j=0; printf("请输入n的值:\n"); scanf("%d",&n); for(i=1;i<=n;i++) { j=j+i; } printf("%d+%d+%d.+1=%d\n",n,n-1,n-2,j); } 下面的是阶乘的:#include<stdio.h> main() { int n,i,j=1;

一:正整数前n个的和:1++2+3+4+5+..+n=? n+n-1+n-2++1=? 二者相等 首项与尾项的和n+1,共有n组 并且计算过两次 则最后的结果是: n(n+1)/2 二:同上面的方法类似从 2+4++2n=? 2n+2(n-1+2=? 首项与尾项的和2n+2 ,共有n组 之和n(2n+2)/2=n(n+1)

an = n^2= n(n+1) -n=(1/3)[ n(n+1)(n+2) -(n-1)n(n+1) ] - (1/2)[n(n+1) -(n-1) n]Sn =a1+a2++an=(1/3)n(n+1)(n+2) - (1/2)n(n+1)=(1/6)n(n+1)[ 2(n+2) -3] =(1/6)n(n+1)( 2n+1)

直接相加,没简便算法.但其极限等于自然对数e.

Sn=(a1+an)n/2Sn=[a1(1+q^n)]/1+q

#include<stdio.h> int main() { double sum = 0, a = 1, b = 2; int n; scanf("%d", &n); for (int i = 0;i < n;i++) { sum += b / a; b += a; a = b - a; } printf("%.4f\n",sum); return 0; }

网站首页 | 网站地图
All rights reserved Powered by www.phfb.net
copyright ©right 2010-2021。
内容来自网络,如有侵犯请联系客服。zhit325@qq.com