數(shù)列求和求積_第1頁
數(shù)列求和求積_第2頁
數(shù)列求和求積_第3頁
數(shù)列求和求積_第4頁
數(shù)列求和求積_第5頁
已閱讀5頁,還剩15頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領

文檔簡介

1、 數(shù)列求和求積: - 累加累乘遞推法 基本累加/累乘問題 累加法:求和變量初值一般為0,每循環(huán)一次,求和變量自加一個數(shù)據(jù),這樣循環(huán)結(jié)束后,求和變量的值即為這些數(shù)據(jù)的和。 累乘法:累乘變量初值一般為1,每循環(huán)一次,累乘變量自乘一個數(shù)據(jù),這樣當循環(huán)結(jié)束的時候,累乘變量的值即為這些數(shù)據(jù)連乘的積。 niniinpins11.321.321基本累加: void main() int j,n,s=0; scanf(“%d”,&n);for(j=0;jn;j+)s = s+n;printf(“%d”, s); 基本累乘:基本累乘:void main() int j,n,s=1; scanf(“%d”

2、,&n);for(j=0;jn;j+)s = s*n;printf(“%d”, s); 例1: 輸入n個100分制成績(用整型),計算并輸出平均成績,要求輸出精確到兩位小數(shù)。 #include “stdio.h”#define n 10void main() double av=0; int score,i;printf(“Input %d int numbers:”, n);for(i=1; i=n; i+) scanf(“%d”, &score); av+=score; av/=n;printf(“av=%.2fn”,av);例2:已知s=1!+2!+3!+n! ,求當s首

3、次超過 2,000,000時的 n 和 s 的值void main()long n=1, an=1;long s=0;while(s2000000)an=an*n;s=s+an;n+;printf(“%ld %ld”,s,n);例3:用公式 求 的近似值,直到余項的首項絕對值小于10-6為止#include void main ( ) float PI=0., m=1., i=1.; int j= -1; do PI=PI+m; i=i+2; m=j/i; j= - j; while (fabs (m)= 0.000001); PI=4*PI; printf(“PI=%fn”, PI);.71

4、513114例4:求 的部分和。直到余項的首項的值小于1.0e-6 void main ( ) float x, u=1., s=0.; int n=0; scanf(”%f”, &x); do s+ = u; u*= x/(n+1); n+; while (u=1.e-6); printf(“exp(x)=%fn”, s ); .!.! 212nxxxenx例5: 求和 s=sin1+sin2+.+sin(n)(求scos 1+cos 2+cos n 與此類似)#include void main ( )int i,n; float s=0;scanf(“%d”,&n); f

5、or ( i=1; i=n; i+) s+=sin(i); printf (“s=%fn”, s );例6: 計算 s = 1+2/3 - 4/5+6/7 - .+3998/3999void main()int k;float s=1.;float u=1.;for(k=1; k1e-6) s+=a; k+=2; a*=-x*x/k/(k-1); printf(“s=%.6fn”,s);! 6! 4! 21)cos(642xxxxs例8: 計算 fibonacci 數(shù)列:1,1,2,3,5,8,13,的前40個數(shù)main()long a=1, b=1; int i;for(i=1; i=20;

6、 i+)printf(“%16ld %16ld”, a, b);if(i%2=0) printf(“n”);a=b+a;b=a+b;例9: 輸入n, m, 計算組合數(shù) P=C(n, m) mkmnkkmnmnmnC1)!( !#include “stdio.h”void main()double p=1.0; int m,n,k;printf(“Input n,m:”); scanf(“%d%d”, &n, &m);if(n0|mn) printf(“Input data arent correct.n”); return; if(n-mm) m=n-m; for(k=1; k

7、=m; k+) p*=(double)(n-m+k)/k; printf(“c(%d,%d)=%.0fn”, n, m, p);例例10#include “stdio.h”void main() int n, i; double a, x, y;printf(“Input x,n:”); scanf(“%lf%d”, &x, &n);printf(“Input a0, a1,a%dn, n);y=0;for(i=0;i=1e-5) s=s+m; i=i+1; j=-j; f=f*x*x; f1=f1+2; f2=f2*i; m=j*f(f1+f2); printf(“s=%fn

8、”,s);.! 3*7!2*5! 1*3)(753xxxxxs例12:輸入一行 16 進制字符串,變成長整數(shù)輸出 #include “stdio.h”void main()long y=0; int x=16, a; char c;printf(“Input a hex number:”); while(1) c=getchar(); if(c=n) break;if(c=0&c=a&c=A&c=Z) a=c-55;else break;y=y*x+a;printf(“y=%ldn”,y); 1. 求s=a+aa+aaa+aaaa之值,其中a是一個數(shù)字。例如:2+22+222+2222+22222(此時n=5),n由鍵盤輸入。2. 有一分數(shù)序列: 求出這個數(shù)列的前20項之和。,.1321,813,58,35,23,12void main(int a, n, count=1, sn=0, tn=0;printf(“input a and n:”);scanf(“%d %d”, &a, &n);printf(“a=%d, n=%dn”, a, n);while(count =n)tn=tn+a;sn=sn+tn;a=a*10;+count;printf

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責。
  • 6. 下載文件中如有侵權(quán)或不適當內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論