C語言經(jīng)典算法27_第1頁
免費預覽已結(jié)束,剩余9頁可下載查看

下載本文檔

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

文檔簡介

1、c語言經(jīng)典算法27題目:利用遞歸函數(shù)調(diào)用方式,將所輸入的5個字符,以相反挨次打印出來。1.程序分析:2.程序源代碼:include "stdio.h"main()int i=5;void palin(int n);printf("40:");palin(i);printf("n");void palin(n)int n;char next;if(n next=getchar();printf("n0:");putchar(next);elsenext=getchar();palin(n-1);putchar(nex

2、t);=【程序28】題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數(shù),他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后問第一個人,他說是10歲。請問第五個人多大?1.程序分析:利用遞歸的辦法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲數(shù),需知道第四人的歲數(shù),依次類推,推到第一人(10歲),再往回推。2.程序源代碼:age(n)int n;int c;if(n=1) c=10;else c=age(n-1)+2;return(c);main() printf("%d",age(5);=【程序29】題目

3、:給一個不多于5位的正整數(shù),要求:一、求它是幾位數(shù),二、逆序打印出各位數(shù)字。1. 程序分析:學會分解出每一位數(shù),如下說明:(這里是一種容易的算法,師專數(shù)002班趙鑫提供)2.程序源代碼:main( )long a,b,c,d,e,x;scanf("%ld",a=x/10000;/*分解出萬位*/b=x%10000/1000;/*分解出千位*/c=x%1000/100;/*分解出百位*/d=x%100/10;/*分解出十位*/e=x%10;/*分解出個位*/if (a!=0) printf("there are 5, %ld %ld %ld %ld %ldn&quo

4、t;,e,d,c,b,a);else if (b!=0) printf("there are 4, %ld %ld %ld %ldn",e,d,c,b);else if (c!=0) printf(" there are 3,%ld %ld %ldn",e,d,c);else if (d!=0) printf("there are 2, %ld %ldn",e,d);else if (e!=0) printf(" there are 1,%ldn",e);=【程序30】題目:一個5位數(shù),推斷它是不是回文數(shù)。即123

5、21是回文數(shù),個位與萬位相同,十位與千位相同。1.程序分析:同29例2.程序源代碼:main( )long ge,shi,qian,wan,x;scanf("%ld",wan=x/10000;qian=x%10000/1000;shi=x%100/10;ge=x%10;if (ge=wan&&shi=qian)/*個位等于萬位并且十位等于千位*/printf("this number is a huiwenn");elseprintf("this number is not a huiwenn");【程序31】題目:請

6、輸入星期幾的第一個字母來推斷一下是星期幾,假如第一個字母一樣,則繼續(xù)推斷其次個字母。1.程序分析:用狀況語句比較好,假如第一個字母一樣,則推斷用狀況語句或if語句推斷其次個字母。2.程序源代碼:includevoid main()char letter;printf("please input the first letter of somedayn");while (letter=getch()!='y')/*當所按字母為y時才結(jié)束*/ switch (letter)case 's':printf("please input se

7、cond lettern");if(letter=getch()='a')printf("saturdayn");else if (letter=getch()='u')printf("sundayn");else printf("data errorn");break;case 'f':printf("fridayn");break;case 'm':printf("mondayn");break;case 't

8、':printf("please input second lettern");if(letter=getch()='u')printf("tuesdayn");else if (letter=getch()='h')printf("thursdayn");else printf("data errorn");break;case 'w':printf("wednesdayn");break;default: printf("da

9、ta errorn");=【程序32】題目:press any key to change color, do you want to try it. please hurry up!1.程序分析:2.程序源代碼:includevoid main(void)int color;for (color = 0; color textbackground(color);/*設置文本的背景色彩*/cprintf("this is color %drn", color);cprintf("press any key to continuern");get

10、ch();/*輸入字符看不見*/=【程序33】題目:學習gotoxy()與clrscr()函數(shù)1.程序分析:2.程序源代碼:includevoid main(void)clrscr();/*清屏函數(shù)*/textbackground(2);gotoxy(1, 5);/*定位函數(shù)*/cprintf("output at row 5 column 1n");textbackground(3);gotoxy(20, 10);cprintf("output at row 10 column 20n");=【程序34】題目:練習函數(shù)調(diào)用1. 程序分析:2.程序源代碼

11、:includevoid hello_world(void)printf("hello, world!n");void three_hellos(void)int counter;for (counter = 1; counter 調(diào)用此函數(shù)*/void main(void)three_hellos();/*調(diào)用此函數(shù)*/=【程序35】題目:文本色彩設置1.程序分析:2.程序源代碼:includevoid main(void)int color;for (color = 1; color textcolor(color);/*設置文本色彩*/cprintf("th

12、is is color %drn", color);textcolor(128 + 15);cprintf("this is blinkingrn");=【程序36】題目:求100之內(nèi)的素數(shù)1.程序分析:2.程序源代碼:includeinclude "math.h"define n 101main()int i,j,line,an;for(i=2;ifor(i=2;i for(j=i+1;j if(a!=0&&aj!=0)if(aj%a=0)aj=0;printf("n");for(i=2,line=0;ii

13、f(a!=0)printf("%5d",a);line+;if(line=10)printf("n");line=0;=【程序37】題目:對10個數(shù)舉行排序1.程序分析:可以利用挑選法,即從后9個比較過程中,挑選一個最小的與第一個元素交換,下次類推,即用其次個元素與后8個舉行比較,并舉行交換。2.程序源代碼:define n 10main()int i,j,min,tem,an;/*input data*/printf("please input ten num:n");for(i=0;iprintf("a%d=",i);scanf("%d", printf("n");for(i=0;iprintf("%5d",

溫馨提示

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

評論

0/150

提交評論