浙江大學(xué)C顏暉原版C5_第1頁
浙江大學(xué)C顏暉原版C5_第2頁
浙江大學(xué)C顏暉原版C5_第3頁
浙江大學(xué)C顏暉原版C5_第4頁
浙江大學(xué)C顏暉原版C5_第5頁
已閱讀5頁,還剩34頁未讀, 繼續(xù)免費閱讀

下載本文檔

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

文檔簡介

1、第5章 循環(huán)結(jié)構(gòu)程序設(shè)計,循環(huán)語句 break 和 continue 語句 循環(huán)嵌套,5.1 循環(huán)語句,問題 打印整數(shù)110 打印整數(shù)1100 打印整數(shù)1n,printf(%d, 1); printf(%d, 2); printf(%d, 10);,i=1; printf(%d, i); i+; printf(%d, i); i+; printf(%d, i); i+;,i=1; while(i sum sum+3 sum sum+10 sum,sum=sum+?,sum=0; i=1; while(i=10) sum=sum+i; i+; ,循環(huán)不變式,for 語句,for (exp1; e

2、xp2; exp3) 語句;,exp3,exp2,非0,0,exp1,語 句,循環(huán)體,while (表達(dá)式) 語句;,for (exp1; exp2; exp3) 語句;,exp1; while(exp2) 語句; exp3; ,while 和 for,while 和 for,while (表達(dá)式) 語句;,for (exp1; exp2; exp3) 語句;,exp1; while(exp2 語句; exp3; ,sum=0; i=1; while(i=10) sum=sum+i; i+; ,sum=0; for(i=1; i=10; i+) sum=sum+i;,程序舉例,例T1-1 求1

3、+2+3+4+ n 例T1-2 求1+1/2+1/3+1/4+ 1/n 例T1-3 求1-1/2+1/3-1/4+ 1/n 例T1-4 求1-1/3+1/5-1/6+ 前n項之和 例T2-1 求n! 例T2-2 求xn 例T3-1 求1-1/3+1/5-1/7+ ,直到最后1項的絕對值10-5 例T4-1 輸入100個整數(shù),求其中正數(shù)之和 例T4-2 輸入一個正整數(shù)n, 再輸入n個數(shù),輸出最大值 例T5 輸出Fibonacci序列前20個數(shù),例T1-1 求1+2+3+4+ n,算法: i =1 to n s=s+t t+ i+,程序段: s=0; for(i=1; i=n; i+) s=s+i

4、;,算法: i =1 to n s=s+i i+,例T1-2 求1+1/2+1/3+ 1/n,算法: i =1 to n s=s+t t=1.0/i i+,程序段: s=0; for(i=1; i=n; i+) s=s+1.0/i;,算法: i =1 to n s=s+t t+ i+,例T1-3 求1-1/2+1/3-1/4+ 1/n,i =1 to n s=s+t t=1.0/i i+,程序段: s=0; flag=1; for(i=1; i=n; i+) s=s+1.0/i*flag; flag=-flag; ,算法: i =1 to n s=s+t t=1.0/i*flag flag=-

5、flag i+,例T1-4 求1-1/3+1/5- 前n項和,算法: i =1 to n s=s+t t=1.0/i*flag flag=-flag i+,程序段: s=0;flag=1;tt=1; for(i=1; i=n; i+) s=s+1.0/tt*flag; tt+=2; flag=-flag; ,算法: i =1 to n s=s+t t=1.0/tt*flag flag=-flag tt=tt+2 i+,例T2-1 求n!,算法: i =1 to n f=f*t t+ i+,程序段: f=1; for(i=1; i=n; i+) f=f*i;,算法: i =1 to n f=f*

6、i i+,例T2-2 求xn,算法: i =1 to n f=f*t t+ i+,程序段: f=1; for(i=1; i=1E-5 s=s+t flag=-flag tt=tt+2 t=1.0/tt*flag,例T4-1 輸入100個整數(shù),求其中正數(shù)之和,# include void main() int i , sum=0, x; for (i=0; i0) sum=sum+x; printf(%d,sum); ,例47 求最大值,輸入3個數(shù),輸出其中的最大值。 #include void main( ) int a, b, c, max; printf(input a, b, c:n);

7、 scanf(%d%d%d, ,a max b c,max,max,例T4-1輸入一個正整數(shù)n, 再輸入n個數(shù),輸出最大值,void main() int i , max, n, x; scanf(%d, ,max,x,例T5 輸出Fibonacci序列前20個數(shù),1, 1, 2, 3, 5, 8, x1 x2 t x1 x2 t,程序段: x1=x2=1; printf(%d %d, x1, x2); for(i=1; i=18; i+) t=x1+x2; printf(%d , t); x1=x2; x2=t; ,x1=x2=1; t=x1+x2; x1=x2; x2=t;,do-whil

8、e 語句,do 語句 while (表達(dá)式);,i=1; do printf(%d, i); i+; while(i=10);,語 句,表達(dá)式,非0,0,while (表達(dá)式) 語句;,while 和 do-while,do 語句 while (表達(dá)式);,先循環(huán),后判斷,先判斷,后循環(huán),while 和 do-while 的用法比較,輸入一些數(shù),求和,直到輸入負(fù)數(shù)為止。 void main( ) int x, sum=0; do scanf(%d, ,void main( ) int x, sum=0; scanf(%d, ,輸入 1 2 5 -10,輸入 -10 1 2 5,sum-x,5.

9、2 break 和 continue 語句,#include stdio.h void main( ) char c; int i=0; for (i=0; i10;i+) c=getchar(); if (c=n) break; putchar(c); ,循環(huán)何時結(jié)束?,c=getchar(); for (i=0; i= 10 | c = n) break; putchar(c); ,1,break 流程,結(jié)束循環(huán) while(exp) 語句1 if (expb) break; 語句2 ,continue 流程,跳過continue后面的語句,繼續(xù)下一次循環(huán) while(exp) 語句1 i

10、f (expc) continue; 語句2 ,break 和 continue,#include stdio.h void main( ) char c; int i=0; for (i=0; i=a ,while(c = getchar()!=n),5.3 循環(huán)嵌套,while (表達(dá)式) 語句;,例T9-1.1 1!+2!+n!,s=0; for(k=1; k=n; k+) s=s+f; ,f=1; /* n! */ for(i=1; i=n; i+) f=f*i;,f=1; for(i=1; i=k; i+) f=f*i;,算法: k=1 to n s = s + f f = k! k

11、+,例T9-1.2 1!+2!+n!,算法: k=1 to n s = s + f f = k! k+,s=0;f=1; for(k=1;k=n;k+) f=f*k; s=s+f; ,算法: k =1 to n s = s+f f = f*k k+,例T9-2 兌換零錢,將10元錢換成5角、2角、1角的零錢(至少各一枚),列出所有可能的方案。 c5: 5角的數(shù)量, 1, 20) c2: 2角的數(shù)量, 1, 50) c1: 1角的數(shù)量, 1, 100) 5*c1+2*c2+c5=100,將10元錢換成5角、2角、1角的零錢 c5: 5角的數(shù)量, 1, 20) c2: 2角的數(shù)量, 1, 50)

12、c1: 1角的數(shù)量, 1, 100) 5*c5+2*c2+c1=100,for (c5=1; c520; c5+) for (c2=1; c250; c2+) for (c1=1; c1100; c1+) if (5*c5+2*c2+c1=100 ) printf(%d %d %dn, c5, c2, c1);,c5=1 c2=1 c1=1, 99,2, 49,2, 19,將10元錢換成5角、2角、1角的零錢 c5: 5角的數(shù)量, 1, 20) c2: 2角的數(shù)量, 1, 50) c1: 1角的數(shù)量, 1, 100) 5*c5+2*c2+c1=1000,for (c5=1; c520; c5+) for (c2=1; c250; c2+) for (c1=1; c1100; c1+) if (5*c5+2*c2+c1=1000 ) printf(%d %d %dn, c5, c2, c1);,for (c5=1; c520; c5+) for (c2=1; c250; c2+) printf(%d %d %dn, c5, c2, 1000- 5*c5+2*c2);,例T9-3 輸出100200間所有素數(shù),算法: m =100 to 200 if m是素數(shù) print m,n=sqrt(m); for(i

溫馨提示

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

評論

0/150

提交評論