版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)
文檔簡介
【程序16]題目:輸入兩個正整數(shù)m和n,求其最大公約數(shù)和最小公倍數(shù)。.程序分析:利用輾除法。.程序源代碼:main()(inta,b,numl,num2,temp;printf("pleaseinputtwonumbers:\n");scanf("%d,%d",&numl,&num2);if(numl{temp=numl;numl=num2;num2=temp;}a=numl;b=num2;while(b!=0)/*利用輾除法,直到b為0為止*/(temp=a%b;a=b;b=temp;)printf("gongyueshu:%d\n",a);printf("gongbeishu:%d\n",numl*num2/a);【程序19]題目:一個數(shù)如果恰好等于它的因子之和,這個數(shù)就稱為“完數(shù)二例如6=1+2+3.編程找出1000以內(nèi)的所有完數(shù)。.程序分析:請參照程序(-上頁程序14..程序源代碼:main()(staticintk[10];intij,n,s;for(j=2;j<1000;j++)(n=-l;s=j;for(i=l;i{if((j%i)==0){n++;s=s-i;k[n]=i;}if(s==O)(printf("%disawanshu"J);for(i=0;iprintf("%d,",k。]);printf("%d\n",k[n]);}}}【程序20]題目:一球從100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在第10次落地時,共經(jīng)過多少米?第10次反彈多高?.程序分析:見下面注釋.程序源代碼:main()(floatsn=100.0,hn=sn/2;intn;for(n=2;n<=10;n++)sn=sn+2*hn;/*第n次落地時共經(jīng)過的米數(shù)*/hn=hn/2;/*第n次反跳高度*/)printf("thetotalofroadis%f\n",sn);printf("thetenthis%fmeter\n",hn);}【程序21]題目:猴子吃桃問題:猴子第一天摘下若干個桃子,當(dāng)即吃了一半,還不癮,又多吃了一個第二天早上又將剩下的桃子吃掉一半,又多吃了一個。以后每天早上都吃了前一天剩下的一半零一個。到第10天早上想再吃時,見只剩下一個桃子了。求第一天共摘了多少。.程序分析:采取逆向思維的方法,從后往前推斷。.程序源代碼:main()(intday,xl,x2;day=9;x2=l;while(day>0)僅l=(x2+l)*2;/*第一天的桃子數(shù)是第2天桃子數(shù)加1后的2倍*/x2=xl;day-;)printf("thetotalis%d\n",xl);}【程序22]題目:兩個乒乓球隊進(jìn)行比賽,各出三人。甲隊為a,b,c三人,乙隊為x,y,z三人。已抽簽決定比賽名單。有人向隊員打聽比賽的名單。a說他不和x比,c說他不和x,z比,請編程序找出三隊賽手的名單。.程序分析:判斷素數(shù)的方法:用一個數(shù)分別去除2到sqrt(這個數(shù)),如果能被整除,則表明此數(shù)不是素數(shù),反之是素數(shù)。.程序源代碼:main()char 是a的對手,j是b的對手,k是c的對手*/for(i='x,;i<='z';i++)for(j='x';j<='z,;j++)(if(i!=j)for(k='x';k<=,z';k++){if(i!=k&&j!=k){if(i!='x,&&k!='x,&&k!=,z,)printf("orderisa-%c\tb-%c\tc-%c\n",i,j,k);}}}}【程序23]題目:打印出如下圖案(菱形)*******.程序分析:先把圖形分成兩部分來看待,前四行一個規(guī)律,后三行一個規(guī)律,利用雙重for循環(huán),第一層控制行,第二層控制列。.程序源代碼:main()(inti,j,k;for(i=0;i<=3;i++)(for(j=0;j<=2-i;j++)printf("");for(k=0;k<=2*i;k++)printf("*");printf("\n");}for(i=0;i<=2;i++)(for(j=0;j<=i;j++)printf("");for(k=0;k<=4-2*i;k++)【程序24]題目:有一分?jǐn)?shù)序列:2/1,3/2,5/3,8/5,13/8,21/13…求出這個數(shù)列的前20項(xiàng)之和。.程序分析:請抓住分子與分母的變化規(guī)律。.程序源代碼:main()(intn,t,number=20;floata=2,b=l,s=0;for(n=l;n<=number;n++)(s=s+a/b;t=a;a=a+b;b=t;/*這部分是程序的關(guān)鍵,請讀者猜猜t的作用*/}printf("sumis%9.6f\n",s);【程序25]題目:求1+2!+3!+...+20!的和.程序分析:此程序只是把累加變成了累乘。.程序源代碼:main()(floatn,s=0,t=l;for(n=l;n<=20;n++){t*=n;s+=t;}printf("l+2!+3!...+20!=%e\n"/s);}【程序26]題目:利用遞歸方法求5!。.程序分析:遞歸公式:fn=fn_l*4!.程序源代碼:#include"stdio.h"main()inti;intfact();for(i=0;i<5;i++)printf("\40:%d!=%d\n",i/fact(i));}intfact(j)intj;{intsum;if(j==O)sum=l;elsesum=j*fact(j-l);returnsum;}【程序27]題目:利用遞歸函數(shù)調(diào)用方式,將所輸入的5個字符,以相反順序打印出來。.程序分析:.程序源代碼:include"stdio.h"main()(inti=5;voidpalin(intn);printf("\40:");palin(i);printf("\n");}voidpalin(n)intn;(charnext;if(n<=l)(next=getchar();printf("\n\0:");putchar(next);elsenext=getchar();palin(n-l);putchar(next);)}【程序28]題目:有5個人坐在一起,問第五個人多少歲?他說比第4個人大2歲。問第4個人歲數(shù),他說比第3個人大2歲。問第三個人,又說比第2人大兩歲。問第2個人,說比第一個人大兩歲。最后問第一個人,他說是10歲。請問第五個人多大?.程序分析:利用遞歸的方法,遞歸分為回推和遞推兩個階段。要想知道第五個人歲數(shù),需知道第四人的歲數(shù),依次類推,推到第一人(10歲),再往回推。.程序源代碼:age(n)intn;intc;if(n==l)c=10;elsec=age(n-l)+2;return(c);}main(){printf("%d”,age(5));}【程序29]題目:給一個不多于5位的正整數(shù),要求:一、求它是幾位數(shù),二、逆序打印出各位數(shù)字。.程序分析:學(xué)會分解出每一位數(shù),如下解釋:(這里是一種簡單的算法,師專數(shù)002班趙鑫提供).程序源代碼:main()(longa,b,c,d,e,x;scanf("%lcT,&x);a=x/10000;/*分解出萬位*/b=x000/1000;/*分解出千位*/c=x00/100;/*分解出百位*/d=x0/10;/*分解出十位*/e=x;/*分解出個位*/if(a!=0)printf("thereare5,%ld%ld%ld%ld%ld\n";e,d,c,b,a);elseif(b!=0)printf("thereare4,%ld%ld%ld%ld\n";e,d,c;b);elseif(c!=0)printf("thereare3,%ld%ld%ld\n",e,d,c);elseif(d!=0)printf("thereare2,%ld%ld\n",e,d);elseif(e!=0)printf("therearel;%ld\n",e);}【程序30]題目:一個5位數(shù),判斷它是不是回文數(shù)。即12321是回文數(shù),個位與萬位相同,十位與千位相同。.程序分析:同29例.程序源代碼:main()(longge,shi,qian,wan,x;scanf("%ld",&x);wan=x/10000;qian=x000/1000;shi=x0/10;ge=x;if(ge==wan&&shi==qian)/*個位等于萬位并且十位等于千位*/printf("thisnumberisahuiwen\n");elseprintf("thisnumberisnotahuiwen\n");}【程序31]題目:請輸入星期幾的第一個字母來判斷一下是星期幾,如果第一個字母一樣,則繼續(xù)判斷第二個字母。.程序分析:用情況語句比較好,如果第一個字母一樣,則判斷用情況語句或if語句判斷第二個字母。.程序源代碼:#includevoidmain()(charletter;printf("pleaseinputthefirstletterofsomeday\n");while((letter=getch())!=Y)/*當(dāng)所按字母為Y時才結(jié)束*/{switch(letter){case'S':printf("pleaseinputsecondletter\n");if((letter=getch())=='a')printf("saturday\n");elseif((letter=getch())=='u')printf("sunday\n");elseprintf("dataerror\n");break;case'F':printf("friday\n");break;case,M,:printf("monday\n");break;case'TiprintfC'pleaseinputsecondletter\n");if((letter=getch())=='u,)printf("tuesday\n");elseif((letter=getch())=='h')printf("thursday\n");elseprintf("dataerror\n");break;case,W,:printf("wednesday\n");break;default:printf("dataerror\n");}}}【程序32]題目:Pressanykeytochangecolor,doyouwanttotryit.Pleasehurryup!.程序分析:.程序源代碼:#includevoidmain(void)(intcolor;for(color=0;color<8;color++){textbackground(color);/*設(shè)置文本的背景顏色*/cprintf("Thisiscolor%d\r\n",color);cprintf("Pressanykeytocontinue\r\n");getch();/*輸入字符看不見*/}}【程序33]題目:學(xué)習(xí)gotoxy()與drscr()函數(shù).程序分析:.程序源代碼:#includevoidmain(void)(drscr。;/*清屏函數(shù)*/textbackground(2);gotoxy(l,5);/*定位函數(shù)*/cprintf("Outputatrow5columnl\n");textbackground(3);gotoxy(20,10);cprintf("Outputatrow10column20\n");}【程序34]題目:練習(xí)函數(shù)調(diào)用.程序分析:.程序源代碼:#includevoidhello_world(void)(printfC'Hello,world!\n");voidthree_hellos(void)intcounter;for(counter=1;counter<=3;counter++)hello_world();/*調(diào)用此函數(shù)*/}voidmain(void){three_hellos();/*調(diào)用此函數(shù)*/}【程序35]題目:文本顏色設(shè)置.程序分析:.程序源代碼:#includevoidmain(void)(intcolor;for(color=1;color<16;color++)textcolor(color);/*設(shè)置文本顏色*/cprintf("Thisiscolor%d\r\n",color);}textcolor(128+15);cprintf("Thisisblinking\r\n");}【程序36]題目:求100之內(nèi)的素數(shù).程序分析:.程序源代碼:#include/include"math.h"#defineN101main()(inti,j,line,a[N];for(i=2;ifor(i=2;ifor(j=i+l;j{if(a[i]!=O&&a[j]!=O)if(a[j]%a[i]==O)a[j]=O;}printf("\n");for(i=2,line=0;i{if(a[i]!=O){printf("]",a[i]);line++;}if(line==10){printf("\n");line=O;}}}【程序37]題目:對10個數(shù)進(jìn)行排序.程序分析:可以利用選擇法,即從后9個比較過程中,選擇一個最小的與第一個元素交換,下次類推,即用第二個元素與后8個進(jìn)行比較,并進(jìn)行交換。.程序源代碼:#defineN10main(){inti,j;min,tem,a[N];/*inputdata*/printf("pleaseinputtennum:\n");for(i=0;i{printf("a[%d]=",i);scanf("%d",&a[i]);}printf("\n");for(i=0;iprintf("]",a[i]);printf("\n");/*sorttennum*/for(i=0;i{min=i;for(j=i+l;jif(a[min]>a[j])min=j;tem=a[i];a[i]=a[min];a[min]=tem;}/*outputdata*/printf("Aftersorted\n");for(i=0;iprintf("]'',a[i]);}【程序38]題目:求一個3*3矩陣對角線元素之和.程序分析:利用雙重for循環(huán)控制輸入二維數(shù)組,再將a[i][i]累加后輸出。.程序源代碼:main()(floata[3][3],sum=0;intij;printf("pleaseinputrectangleelement:\n");for(i=0;i<3;i++)for(j=0;j<3;j++)scanf("%f",&a[i]D]);for(i=0;i<3;i++)sum=sum+a[i][i];printf("duijiaoxianheis%6.2f",sum);}【程序39]題目:有一個已經(jīng)排好序的數(shù)組?,F(xiàn)輸入一個數(shù),要求按原來的規(guī)律將它插入數(shù)組中。.程序分析:首先判斷此數(shù)是否大于最后一個數(shù),然后再考慮插入中間的數(shù)的情況,插入后此元素之后的數(shù),依次后移一個位置。.程序源代碼:main()(inta口1]={1,4,6,9,13,16,19,28,40,100};inttempi,temp2,number,end,i,j;printf("originalarrayis:\n");for(i=0;i<10;i++)printf("]"za[i]);printf("\n");printf("insertanewnumber:");scanf("%d",&number);end=a[9];if(number>end)a[10]=number;else{for(i=0;i<10;i++){if(a[i]>number){templ=a[i];a[i]=number;for(j=i+l;j<ll;j++){temp2=a[j];a[j]=templ;templ=temp2;}break;)}}for(i=0;i<ll;i++)printf("m",a[i]);}【程序41]題目:學(xué)習(xí)static定義靜態(tài)變量的用法.程序分析:.程序源代碼:include"stdio.h"varfunc()(intvar=0;staticintstatic_var=O;printf("\40:varequal%d\n",var);printf("\40:staticvarequal%d\n",static_var);printf("\n");var++;static_var++;)voidmain(){inti;for(i=0;i<3;i++)varfunc();}【程序42]題目:學(xué)習(xí)使用auto定義變量的用法.程序分析:.程序源代碼:include"stdio.h"main(){inti,num;num=2;for(i=0;i<3;i++){printf("\40:Thenumequal%d\n",num);num++;autointnum=l;printf("\40:Theinternalblocknumequal%d\n",num);num++;}}}【程序43]題目:學(xué)習(xí)使用static的另一用法。.程序分析:.程序源代碼:#include"stdio.h"main()(inti,num;num=2;for(i=0;i<3;i++)printf("\40:Thenumequal%d\n",num);num++;staticintnum=l;printf("\40:Theinternalblocknumequal%d\n",num);num++;}}}【程序44]題目:學(xué)習(xí)使用external的用法。.程序分析:.程序源代碼:#include"stdio.h"inta,b,c;voidadd(){inta;a=3;c=a+b;voidmain(){a=b=4;add();printf("Thevalueofcisequalto%d\n",c);}【程序45]題目:學(xué)習(xí)使用register定義變量的方法。.程序分析:.程序源代碼:voidmain()(registerinti;inttmp=0;for(i=l;i<=100;i++)tmp+=i;printf("Thesumis%d\n",tmp);}【程序46]題目:宏#define命令練習(xí)⑴.程序分析:.程序源代碼:include"stdio.h"#defineTRUE1#defineFALSE0#defineSQ(x)(x)*(x)voidmain(){intnum;intagain=l;printf("\40:Programwillstopifinputvaluelessthan5O.\n");while(again)(printf("\40:Pleaseinputnumber==>");scanf("%d",&num);printf("\40:Thesquareforthisnumberis%d\n"zSQ(num));if(num>=50)again=TRUE;elseagain=FALSE;【程序47]題目:宏#define命令練習(xí)(2).程序分析:.程序源代碼:#include"stdio.h"#defineexchange(a,b){\/*宏定義中允許包含兩道衣裳命令的情形,此時必須在最右邊加上intt;\t=a;\a=b;\b=t;\}voidmain(void)(intx=10;inty=20;printf("x=%d;y=%d\n"zx,y);exchange(x,y);printf("x=%d;y=%d\n"zx,y);【程序48]題目:宏#define命令練習(xí)⑶.程序分析:.程序源代碼:#defineLAG>#defineSMA<#defineEQ==/include"stdio.h"voidmain(){inti=10;intj=20;if(iLAGj)printf("\40:%dlargerthan%d\n",i,j);elseif(iEQj)printf("\40:%dequalto%d\n",ij);elseif(iSMAj)printf("\40:%dsmallerthan%d\n",i,j);elseprintf("\40:Nosuchvalue.\n");【程序49]題目:#if#ifdef和#ifndef的綜合應(yīng)用。.程序分析:.程序源代碼:#include"stdio.h"#defineMAX#defineMAXIMUM(x,y)(x>y)?x:y#defineMINIMUM(x,y)(x>y)?y:xvoidmain(){inta=10,b=20;#ifdefMAXprintf("\40:Thelargeroneis%d\n",MAXIMUM(a,b));#elseprintf("\40:Theloweroneis%d\n",MINIMUM(a,b));#endif#ifndefMINprintf("\40:Theloweroneis%d\n",MINIMUM(a,b));#elseprintf("\40:Thelargeroneis%d\n",MAXIMUM(a,b));#endif#undefMAX#ifdefMAXprintf("\40:Thelargeroneis%d\n",MAXIMUM(a,b));#elseprintf("\40:Theloweroneis%d\n",MINIMUM(a,b));#endif#defineMIN#ifndefMINprintf("\40:Theloweroneis%d\n",MINIMUM(a,b));#elseprintf("\40:Thelargeroneis%d\n",MAXIMUM(a,b));#endif}【程序50]題目:#include的應(yīng)用練習(xí).程序分析:.程序源代碼:test.h文件如下:#defineLAG>#defineEQ==include"test.h"/*一個新文件50.c,包含test.h*/include"stdio.h"voidmain(){inti=10;intj=20;if(iLAGj)printf("\40:%dlargerthan%delseif(iEQj)printf("\40:%dequalto%d\n",ij);elseif(iSMAj)printf("\40:%dsmallerthan%d\n",i,j);elseprintf("\40:Nosuchvalue.\n");}【程序51]題目:學(xué)習(xí)使用按位與&o.程序分析:0&0=0;0&1=0;1&0=0;1&1=1.程序源代碼:include"stdio.h"main()inta,b;a=077;b=a&3;printf("\40:Thea&b(decimal)is%d\n",b);b&=7;printf("\40:Thea&b(decimal)is%d\n",b);}【程序52]題目:學(xué)習(xí)使用按位或|。.程序分析:0|0=0;0|1=1; 1|1=1.程序源代碼:include"stdio.h"main()(inta,b;a=077;b=a|3;printf("\40:Thea&b(decimal)is%d\n",b);b|=7;printf("\40:Thea&b(decimal)is%d\n",b);【程序53]題目:學(xué)習(xí)使用按位異或八o.程序分析:0Ao=0;0Al=l;lA0=l;lAl=0.程序源代碼:include"stdio.h"main(){inta,b;a=077;b=aA3;printf("\40:Thea&b(decimal)is%d\n",b);bA=7;printf("\40:Thea&b(decimal)is%d\n",b);}【程序54]題目:取一個整數(shù)a從右端開始的4?7位。程序分析:可以這樣考慮:⑴先使a右移4位。(2)設(shè)置一個低4位全為1,其余全為0的數(shù)??捎?(?0<<4)⑶將上面二者進(jìn)行&運(yùn)算。2.程序源代碼:main()(unsigneda,b,c,d;scanf("%o",&a);b=a?4;c=~(~0?4);d=b&c;printf("%o\n%o\n",a,d);}【程序55]題目:學(xué)習(xí)使用按位取反?。L程序分析:?0=1;?1=0;2.程序源代碼:include"stdio.h"main()inta,b;a=234;b=~a;printf("\40:Thea's1complement(decimal)is%d\n",b);a=~a;printf("\40:Thea's1complement(hexidecimal)is%x\n",a);}【程序56]題目:畫圖,學(xué)用circle畫圓形。.程序分析:.程序源代碼:"circle*/include"graphics.h"main(){intdriver,mode,i;floatj=l,k=l;driver=VGA;mode=VGAHI;initgrapht&driveG&mode,"");setbkcolor(YELLOW);for(i=0;i<=25;i++)setcolor(8);circle(310,250,k);k=k+j;j=j+0.3;}}【程序57]題目:畫圖,學(xué)用line畫直線。.程序分析:.程序源代碼:#include"graphics.h"main(){intdriver,modej;floatxO,yO,yl,xl;floatj=12,k;driver=VGA;mode=VGAHI;initgrapht&driveG&mode,"");setbkcolor(GREEN);x0=263;y0=263;yl=275;xl=275;for(i=0;i<=18;i++)setcolor(5);line(xO,yO,xO,yl);x0=x0-5;y0=y0-5;xl=xl+5;yl=yl+5;j=j+10;}x0=263;yl=275;y0=263;for(i=0;i<=20;i++)(setcolor(5);line(xO,yO,xO,yl);x0=x0+5;yO=yO+5;yl=yl-5;}【程序58]題目:畫圖,學(xué)用rectangle畫方形。.程序分析:利用for循環(huán)控制100-999個數(shù),每個數(shù)分解出個位,十位,百位。.程序源代碼:#include"graphics.h"main(){intxO,y0,yl,xl,driver,mode,i;driver=VGA;mode=VGAHI;initgraph(&driver/&mode,"");setbkcolor(YELLOW);x0=263;y0=263;yl=275;xl=275;for(i=0;i<=18;i++)(setcolor(l);rectangle(xO,yO,xl,yl);x0=x0-5;y0=y0-5;xl=xl+5;yl=yl+5;settextstyle(DEFAULT_FONT,HORIZ_DIR/2);outtextxy(150,40,"Howbeautifulitis!");line(130,60,480,60);setcolor(2);circle(269,269,137);)【程序59]題目:畫圖,綜合例子。.程序分析:.程序源代碼:definePAI3.1415926defineB0.809include"graphics.h"#include"math.h"main()(inti,j,k,x0,y0,x,y,driver,mode;floata;driver=CGA;mode=CGACO;initgraph(&driver,&mode;"");setcolor(3);setbkcolor(GREEN);x0=150;y0=100;circle(x0,y0,10);circle(x0,y0,20);circle(x0,y0,50);for(i=0;i<16;i++)(a=(2*PAI/16)*i;x=ceil(x0+48*cos(a));y=ceil(yO+48*sin(a)*B);setcolor(2);line(xO,yO,x,Y);}setcolor(3);circle(x0,y0,60);/*Make0timenormalsizeletters*/settextstyle(DEFAULT_FONT,HORIZ_DIR,0);outtextxy(10,170,"pressakey");getch();setfillstyle(HATCH_FILL,YELLOW);floodfill(202,100,WHITE);getch();for(k=0;k<=500;k++)setcolor(3);for(i=0;i<=16;i++)a=(2*PAI/16)*i+(2*PAI/180)*k;x=ceil(x0+48*cos(a));y=ceil(yO+48+sin(a)*B);setcolor(2);line(xO,yO,x,y);}for(j=l;j<=50;j++)(a=(2*PAI/16)*i+(2*PAI/180)*k-l;x=ceil(x0+48*cos(a));y=ceil(yO+48*sin(a)*B);line(xO,yO,x,y);}}restorecrtmode();}【程序60]題目:畫圖,綜合例子。.程序分析:.程序源代碼:#include"graphics.h"#defineLEFT0#defineTOP0#defineRIGHT639#defineBOTTOM479#defineLINES400#defineMAXCOLOR15main(){intdriver,mode,error;intxl,yl;intx2,y2;intdxl,dyl,dx2,dy2,i=l;intcount=0;intcolor=0;driver=VGA;mode=VGAHI;initgraph(&driver,&mode;"");xl=x2=yl=y2=10;dxl=dyl=2;dx2=dy2=3;while(!kbhit())Iine(xlzyl,x2,y2);xl+=dxl;yl+=dyl;x2+=dx2;y2+dy2;if(xl<=LEFT||xl>=RIGHT)dxl=-dxl;if(yl<=TOP||yl>=BOTTOM)dyl=-dyl;if(x2<=LEFT||x2>=RIGHT)dx2=-dx2;if(y2<=T0P||y2>=B0TT0M)dy2=-dy2;if(++count>LINES)(setcolor(color);color=(color>=MAXCOLOR)?0:++color;}}closegraph();【程序61]題目:打印出楊輝三角形(要求打印出10行如下圖)1.程序分析:111131146415101051.程序源代碼:main(){inti,j;inta[10][10];printf("\n");for(i=0;i<10;i++){a[i][0]=l;a[i][i]=l;}for(i=2;i<10;i++)for(j=l;ja[i][j]=a[i-l]U-l]+a[i-l][j];for(i=0;i<10;i++){for(j=0;j<=i;j++)printf("]",a[i][jD;【程序62]題目:學(xué)習(xí)putpixel畫點(diǎn)。.程序分析:.程序源代碼:/include"stdio.h"#include"graphics.h"main()(inti,j,driver=VGA,mode=VGAHI;initgrapht&driveG&mode,"");setbkcolor(YELLOW);for(i=50;i<=230;i+=20)for(j=50;j<=230;j++)putpixel(i,j,l);for(j=50;j<=230;j+=20)for(i=50;i<=230;i++)【程序63]題目:畫橢圓ellipse.程序分析:.程序源代碼:include"stdio.h"/include"graphics.h"#include"conio.h"main()(intx=360,y=160,driver=VGA,mode=VGAHI;intnum=20,i;inttop,bottom;initgraph(&driver,&mode,"");top=y-30;bottom=y-30;for(i=0;i{ellipse(250,250,0,360,top,bottom);top-=5;bottom+=5;}getch();}【程序64]題目:利用ellipseandrectangle畫圖。.程序分析:.程序源代碼:/include"stdio.h"#include"graphics.h"#include"conio.h"main()(intdriver=VGAzmode=VGAHI;inti,num=15,top=50;intleft=2O,right=5O;initgraph(&driver/&mode,"");for(i=0;i{ellipse(250,250,0,360,rightjeft);ellipse(250,250,0,360,20,top);rectangle(20-2*i/20-2*i,10*(i+2),10*(i+2));right+=5;left+=5;top+=10;}getch();}【程序65]題目:一個最優(yōu)美的圖案。.程序分析:.程序源代碼:#include"graphics.h"/include"math.h"#include"dos.h"include"conio.h"include"stdlib.h"/include"stdio.h"include"stdarg.h"#defineMAXPTS15#definePI3.1415926structPTS{intx,y;};doubleAspectRatio=0.85;voidLineToDemo(void)(structviewporttypevp;structPTSpoints[MAXPTS];inti,j,h,w,xcenter,ycenter;intradius,angle,step;doublerads;printf("MoveTo/LineToDemonstration");getviewsettings(&vp);h=vp.bottom-vp.top;w=vp.right-vp.left;xcenter=w/2;/*Determinethecenterofcircle*/ycenter=h/2;radius=(h-30)/(AspectRatio*2);step=360/MAXPTS;/*Determine#ofincrements*/angle=0;/*Beginatzerodegrees*/for(i=0;irads=(double)angle*PI/180.0;/*Convertangletoradians*/points[i].x=xcenter+(int)(cos(rads)*radius);points[i].y=ycenter-(int)(sin(rads)*radius*AspectRatio);angle+=step;/*Movetonextincrement*/}circle(xcenter,ycenter,radius);/*Drawboundingcircle*/for(i=0;ifor(j=i;jmoveto(points[i].x,points[i].y);/*Movetobeginningofcord*/lineto(pointsU].xzpoints[j].y);/*Drawthecord*/}})main(){intdriver,mode;driver=CGA;mode=CGACO;initgraph(&driver,&mode,"");setcolor(3);setbkcolor(GREEN);LineToDemo();}【程序66]題目:輸入3個數(shù)a,b,c,按大小順序輸出。.程序分析:利用指針方法。.程序源代碼:"pointer*/main()intnl,n2,n3;int*pointeri,*pointer2,*pointer3;printf("pleaseinput3number:nl,n2,n3:");scanf("%d,%d,%cT,&nl,&n2,&n3);pointerl=&nl;pointer2=&n2;pointer3=&n3;if(nl>n2)swap(pointerl,pointer2);if(nl>n3)swap(pointerl,pointer3);if(n2>n3)swap(pointer2,pointer3);printf("thesortednumbersare:%d,%d,%d\n”,nl,n2,n3);}swap(pl,p2)int*pl,*p2;{intp;p=*pl;*pl=*p2;*p2=p;}【程序67]題目:輸入數(shù)組,最大的與第一個元素交換,最小的與最后一個元素交換,輸出數(shù)組。.程序分析:譚浩強(qiáng)的書中答案有問題。.程序源代碼:main()(intnumber[10];input(number);max_min(number);output(number);}input(number)intnumber[10];{inti;for(i=0;i<9;i++)scanf("%d,",&number[i]);scanf("%d",&number[9]);}max_min(array)intarray[10];{int*max,*min,k,l;int*p,*arr_end;arr_end=array+10;max=min=array;for(p=array+l;pif(*p>*max)max=p;elseif(*p<*min)min=p;k=*max;l=*min;*p=array[O];array[O]=l;l=*p;*p=array[9];array[9]=k;k=*p;return;}output(array)intarray[10];{int*p;for(p=array;pprintf("%d/',*p);printf("%d\n",array[9]);}【程序68]題目:有n個整數(shù),使其前面各數(shù)順序向后移m個位置,最后m個數(shù)變成最前面的m個數(shù)1.程序分析:main()intnumber[20],n;mJ;printf("thetotalnumbersis:");scanf("%d",&n);printf("backm:");scanf("%d",&m);for(i=0;iscanf("%d,",&number[i]);scanf("%d",&number[n-l]);move(number,n,m);for(i=0;iprintf("%d,",number[i]);printf("%d"znumber[n-l]);)move(array,n,m)intn,m,array[20];(int*p,array_end;array_end=*(array+n-l);for(p=array+n-l;p>array;p--)*p=*(p-l);*array=array_end;if(m>0)move(array,n,m);}【程序69]題目:有n個人圍成一圈,順序排號。從第一個人開始報數(shù)(從1到3報數(shù)),凡報到3的人退出圈子,問最后留下的是原來第幾號的那位。.程序分析:.程序源代碼:#definenmax50main()(inti,k,m,n,num[nmax],*p;printf("pleaseinputthetotalofnumbers:");scanf("%d",&n);p=num;for(i=0;i*(p+i)=i+l;i=0;k=0;m=0;while(m{if(*(p+i)!=O)k++;if(k==3){*(p+i)=O;k=0;m++;}i++;if(i==n)i=0;}while(*p==O)p++;printf("%disleft\n",*p);}【程序70]題目:寫一個函數(shù),求一個字符串的長度,在main函數(shù)中輸入字符串,并輸出其長度。.程序分析:.程序源代碼:main()intlen;char*str[20];printf("pleaseinputastring:\n");scanf("%s",str);len=length(str);printf("thestringhas%dcharacters."Jen);}length(p)char*p;(intn;n=0;while(*p!='\O')(n++;P++;}returnn;}【程序71]題目:編寫input。和。utput()函數(shù)輸入,輸出5個學(xué)生的數(shù)據(jù)記錄。1.程序分析:#defineN5structstudent{charnum[6];charname[8];intscore[4];}stu[N];input(stu)structstudentstu[];{inti,j;for(i=0;i{printf("\npleaseinput%dof%d\n”,i+l,N);printf("num:");scanf("%s",stu[i].num);printf("name:");scanf("%s",stu[i].name);for(j=0;j<3;j++){printf("score%d."J+l);scanf("%d",&stu[i].score[j]);}printf("\n");)print(stu)structstudentstu[];{inti,j;printf("\nNo.NameScolSco2Sco3\n");for(i=0;i{printf("%-6s%-10s"/stu[i].num,stu[i].name);for(j=0;j<3;j++)printf("%-8d",stu[i].score[j]);printf("\n");}}main()(input();print();}【程序72]題目:創(chuàng)建一個鏈表。.程序分析:.程序源代碼:/*creatalist*/#include"stdlib.h"include"stdio.h"structlist{intdata;structlist*next;);typedefstructlistnode;typedefnode*link;voidmain(){linkptr,head;intnum,i;ptr=(link)malloc(sizeof(node));ptr=head;printf("pleaseinput5numbers==>\n");for(i=0;i<=4;i++)(scanf("%d",&num);ptr->data=num;ptr->next=(link)malloc(sizeof(node));if(i==4)ptr->next=NULL;elseptr=ptr->next;ptr=head;while(ptr!=NULL){printf("Thevalueis==>%d\n",ptr->data);ptr=ptr->next;})【程序73]題目:反向輸出一個鏈表。.程序分析:.程序源代碼:/*reverseoutputalist*/include"stdlib.h"include"stdio.h"structlist{intdata;structlist*next;);typedefstructlistnode;typedefnode*link;voidmain(){linkptr,head,tail;intnum,i;tail=(link)malloc(sizeof(node));tail->next=NULL;ptr=tail;printf("\npleaseinput5data==>\n");for(i=0;i<=4;i++)(scanf("%d",&num);ptr->data=num;head=(link)malloc(sizeof(node));head->next=ptr;ptr=head;}ptr=ptr->next;while(ptr!=NULL){printf("Thevalueis==>%d\n",ptr->data);ptr=ptr->next;})【程序74]題目:連接兩個鏈表。.程序分析:.程序源代碼:include"stdlib.h"#include"stdio.h"structlist{intdata;structlist*next;);typedefstructlistnode;typedefnode*link;linkdelete_node(linkpointerjinktmp){if(tmp==NULL)/*deletefirstnode*/returnpointer->next;else{if(tmp->next->next==NULL)/*deletelastnode*/tmp->next=NULL;else/*deletetheothernode*/tmp->next=tmp->next->next;returnpointer;)}voidselection_sort(linkpointer,intnum){linktmp,btmp;inti,min;for(i=0;i{tmp=pointer;min=tmp->data;btmp=NULL;while(tmp->next){if(min>tmp->next->data){min=tmp->next->data;btmp=tmp;}tmp=tmp->next;}printf("\40:%d\n",min);pointer=delete_node(pointer,btmp);}}linkcreate_list(intarray[],intnum){linktmpl,tmp2,pointer;inti;pointer=(link)malloc(sizeof(node));pointer->data=array[0];tmpl=pointer;for(i=l;i{tmp2=(link)malloc(sizeof(node));tmp2->next=NULL;tmp2->data=array[i];tmpl->next=tmp2;tmpl=tmpl->next;}returnpointer;}linkconcatenate(linkpointerijinkpointer?){linktmp;tmp=pointerl;while(tmp->next)tmp=tmp->next;tmp->next=pointer2;returnpointeri;}voidmain(void){intarrl[]={3/12,8,9/ll};linkptr;ptr=create_list(arrl,5);selection_sort(ptr,5);【程序75]題目:放松一下,算一道簡單的題目。.程序分析:.程序源代碼:main()(inti,n;for(i=l;i<5;i++){n=0;if(i!=l)n=n+l;if(i==3)n=n+l;if(i==4)n=n+l;if(i!=4)n=n+l;if(n==3)printf("zhuhaoshideshi:%c",64+i);【程序76]題目:編寫一個函數(shù),輸入n為偶數(shù)時,調(diào)用函數(shù)求l/2+1/4+...+l/n,當(dāng)輸入n為奇數(shù)時,調(diào)用函數(shù)l/l+l/3+...+l/n(利用指針函數(shù)).程序分析:.程序源代碼:main()#include"stdio.h"main()(floatpeven(),podcl(),dcall();floatsum;intn;while(1)(scanf("%d",&n);if(n>l)break;}if(n%2==0)(printf("Even=");sum=dcall(peven,n);}else{printf("Odd=");sum=dcall(podd;n);}printf("%f',sum);}floatpeven(intn)(floats;inti;s=l;for(i=2;i<=n;i+=2)s+=l/(float)i;return(s);floatpodd(n)intn;(floats;inti;s=0;for(i=l;i<=n;i+=2)s+=l/(float)i;return(s);}floatdcall(fp,n)float(*fp)();intn;(floats;s=(*fp)(n);return(s);}【程序77]題目:填空練習(xí)(指向指針的指針).程序分析:.程序源代碼:main(){char*s[]={"man","woman","girr'/'boy","sister"};char**q;intk;for(k=0;k<5;k++){;/*這里填寫什么語句*/printf("%s\n",*q);}}【程序78]題目:找到年齡最大的人,并輸出。請找出程序中有什么問題。.程序分析:.程序源代碼:#defineN4include"stdio.h"staticstructman{charname[20];intage;}person[N]={"li",18,"wang",19/"zhang"/20,"sun",22};main(){structman*q,*p;inti,m=O;p=person;for(i=O;i{if(mage)q=p++;m=q->age;}printf("%s,%d",(*q).name,(*q).age);}【程序79]題目:字符串排序。.程序分析:.程序源代碼:main()(char*strl[20],*str2[20],*str3[20];charswap();printf("pleaseinputthreestrings\n");scanf("%s",strl);scanf("%s",str2);scanf("%s",str3);if(strcmp(strl,str2)>0)swap(strl,str2);if(strcmp(strl,str3)>0)swap(strl,str3);if(strcmp(str2,str3)>0)swap(str2,str3);printf("afterbeingsorted\n");printf("%s\n%s\n%s\n"/strl/str2,str3);}charswap(pl,p2)char*pl,*p2;(char*p[20];strcpy(p,pl);strcpy(pl,p2);strcpy(p2,p);}【程序80]題目:海灘上有一堆桃子,五只猴子來分。第一只猴子把這堆桃子憑據(jù)分為五份,多了一個,這只猴子把多的一個扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一個,它同樣把多的一個扔入海中,拿走了一份,第三、第四、第五只猴子都是這樣做的,問海灘上原來最少有多少個桃子?.程序分析:.程序源代碼:main(){inti,m,j,k,count;for(i=4;i<10000;i+=4){count=0;m=i;for(k=0;k<5;k++)(j=i/4*5+l;i=j;if(j%4==0)count++;elsebreak;}i=m;if(count==4){printf("%d\n";count);break;}}}【程序81]題目:809*??=800*??+9*??+1其中??代表的兩位數(shù),8*??的結(jié)果為兩位數(shù),9*??的結(jié)果為3位數(shù)。求??代表的兩位數(shù),及809*??后的結(jié)果。.程序分析:.程序源代碼:output(longb,longi){printf("\n%ld/%ld=809*%ld+%ld",b/i,i,b%i);}main(){longinta,b,i;a=809;for(i=10;i<100;i++){b=i*a+l;if(b>=1000&&b<=10000&&8*i<100&&9*i>=100)output(b,i);}}【程序82]題目:八進(jìn)制轉(zhuǎn)換為十進(jìn)制.程序分析:.程序源代碼:main(){char*p,s[6];intn;P=s;gets(p);n=0;while(*(p)!='\O'){n=n*8+*p-'O';P++;}printf("%cT,n);}【程序83]題目:求0—7所能組成的奇數(shù)個數(shù)。.程序分析:.程序源代碼:main()(longsum=4,s=4;intj;for(j=2;j<=8;j++)/*jisplaceofnumber*/{printf(',\n%ld",sum);if(j<=2)s*=7;elses*=8;sum+=s;}printf("\nsum=%ld",sum);}【程序84]題目:一個偶數(shù)總能表示為兩個素數(shù)之和。.程序分析:.程序源代碼:include"stdio.h"include"math.h"main(){inta,b,c,d;scanf("%d",&a);for(b=3;b<=a/2;b+=2){for(c=2;c<=sqrt(b);c++)if(b%c==O)break;if(c>sqrt(b))d=a-b;elsebreak;for(c=2;c<=sqrt(d);c++)if(d%c==O)break;if(c>sqrt(d))printf("%d=%d+%d\n"/a,b,d);}}【程序85]題目:判斷一個素數(shù)能被幾個9整除.程序分析:.程序源代碼:main(){longintm9=9zsum=9;intzi,nl=l,c9=l;scanf("%d",&zi);while(nl!=0){if(!(sum%zi))nl=O;else{m9=m9*10;sum=sum+m9;c9++;}}printf("%ld,canbedividedby%d\"9\"",sum,c9);}【程序86]題目:兩個字符串連接程序.程序分析:.程序源代碼:include"stdio.h"main(){chara[]="acegikm";charb[]="bdfhjlnpq";charc[80]z*p;inti=O,j=O,k=O;while(a[i]!='\O,&&b[j]!='\O'){if(a[i]{c[k]=a[i];i++;}elsec[k]=b[j++];k++;}c[k]='\O';if(a[i]=='\O')P=b+j;elsep=a+i;strcat(c,p);puts(c);}【程序87]題目:回答結(jié)果(結(jié)構(gòu)體變量傳遞).程序分析:.程序源代碼:#include"s
溫馨提示
- 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)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 2026年上半年黑龍江事業(yè)單位聯(lián)考省委辦公廳招聘6人備考考試試題附答案解析
- 2026江西南昌市勞動保障事務(wù)代理中心外包項(xiàng)目招聘2人備考考試題庫附答案解析
- 第14章 回歸:走向自主創(chuàng)新的中國公共行政學(xué)
- 2026年吉安吉星養(yǎng)老服務(wù)有限公司招聘護(hù)理員參考考試題庫附答案解析
- 2026上海應(yīng)物所財務(wù)與資產(chǎn)處副處長競聘1人備考考試試題附答案解析
- 2026中國科學(xué)院理化技術(shù)研究所熱聲熱機(jī)團(tuán)隊招聘特別研究助理博士后1人備考考試試題附答案解析
- 武漢市漢陽區(qū)晴川英才初級中學(xué)招聘教師2人參考考試題庫附答案解析
- (二統(tǒng))紅河州、文山州2026屆高三高中畢業(yè)生第二次復(fù)習(xí)統(tǒng)一檢測地理試卷(含答案解析)
- 2026浙江溫州市平陽縣消防救援大隊廚師招聘1人參考考試試題附答案解析
- 2026山東德州市事業(yè)單位招聘初級綜合類崗位人員備考考試題庫附答案解析
- 變壓器轉(zhuǎn)讓協(xié)議書范本的樣本
- 中考英語688高頻詞大綱詞頻表
- HG∕T 4198-2011 甲醇合成催化劑化學(xué)成分分析方法
- CJT511-2017 鑄鐵檢查井蓋
- 2024年高考語文考前專題訓(xùn)練:現(xiàn)代文閱讀Ⅱ(散文)(解析版)
- 湖南省張家界市永定區(qū)2023-2024學(xué)年七年級上學(xué)期期末考試數(shù)學(xué)試題
- 中醫(yī)外科乳房疾病診療規(guī)范診療指南2023版
- 2023-2024學(xué)年江西省贛州市章貢區(qū)文清實(shí)驗(yàn)學(xué)校數(shù)學(xué)六年級第一學(xué)期期末經(jīng)典模擬試題含答案
- DB36-T 1158-2019 風(fēng)化殼離子吸附型稀土礦產(chǎn)地質(zhì)勘查規(guī)范
- 城市道路照明路燈工程施工組織方案資料
- 雷達(dá)液位計參考課件
評論
0/150
提交評論