C語(yǔ)言編程經(jīng)典及參考答案_第1頁(yè)
C語(yǔ)言編程經(jīng)典及參考答案_第2頁(yè)
C語(yǔ)言編程經(jīng)典及參考答案_第3頁(yè)
C語(yǔ)言編程經(jīng)典及參考答案_第4頁(yè)
C語(yǔ)言編程經(jīng)典及參考答案_第5頁(yè)
已閱讀5頁(yè),還剩34頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

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

文檔簡(jiǎn)介

謀事在人,成事在天!——《增廣賢文》C語(yǔ)言編程經(jīng)典及參考答案1、編程實(shí)現(xiàn)對(duì)鍵盤(pán)輸入的英文名句子進(jìn)行加密。用加密方法為,當(dāng)內(nèi)容為英文字母時(shí)其在26字母中的其后三個(gè)字母代替該字母,若為其它字符時(shí)不變:#include<stdio.h>main(){charc;printf("pleaseinputastring:");while((c=getchar())!='\n'){if(c>='A'&&c<='W'||c>='a'&&c<='w')c=c+3;elseif(c>='X'&&c<='Z'||c>='x'&&c<='z')c=(c+3)-26;printf("%c",c);}printf("\n");return0;}2、編程實(shí)現(xiàn)將任意的十進(jìn)制整數(shù)轉(zhuǎn)換成R進(jìn)制數(shù)(R在2-16之間)。#include<stdio.h>#include<conio.h>voidmain(){intn,R,i=0,a[16]={0};charstr[16]="0123456789ABCDEF";clrscr();printf("pleaseinputanintegernumber:\n");scanf("%d",&n);printf("pleaseinputR:\n");scanf("%d",&R);if(n<0){printf("%c",'-');n=-n;}人人好公,則天下太平;人人營(yíng)私,則天下大亂?!?jiǎng)Ⅸ?/p>

人不知而不慍,不亦君子乎?——《論語(yǔ)》while(n!=0){a[i++]=n%R;n=n/R;}while(i-->0)printf("%c",str[a[i]]);getche();}3、從鍵盤(pán)輸入一指定金額(以元為單位,如345.78),然后顯示支付該金額的各種面額人民幣數(shù)量,要求顯示100元、50元、10元、5元、2元、1元、1角、5分、1分各多少?gòu)?。#include<stdio.h>main(){inta1,a2,a3,a4,a5,a6,a7,a8,a9;floatf;a1=a2=a3=a4=a5=a6=a7=a8=a9=0;printf("pleaseinputafloatnumber:");scanf("%f",&f);while(f>100.0){f=f-100.0;a1++;}while(f>50.0){f=f-50.0;a2++;}while(f>10.0){f=f-10.0;a3++;}while(f>5.0){f=f-5.0;a4++;一寸光陰一寸金,寸金難買(mǎi)寸光陰?!对鰪V賢文》

丈夫志四方,有事先懸弧,焉能鈞三江,終年守菰蒲。——《顧炎武》}while(f>1.0){f=f-1.0;a5++;}while(f>0.5){f=f-0.5;a6++;}while(f>0.1){f=f-0.1;a7++;}while(f>0.05){f=f-0.05;a8++;}while(f>0.01){f=f-0.01;a9++;}printf("a1=%d,a2=%d,a3=%d,a4=%d,a5=%d,a6=%d,a7=%d,a8=%d,a9=%d\n",a1,a2,a3,a4,a5,a6,a7,a8,a9);return0;}4、隨機(jī)產(chǎn)生20個(gè)[10,50]的正整數(shù)存放到數(shù)組中,并求數(shù)組中的所有元素最大值、最小值、平均值及各元素之和。#include<stdlib.h>#include<conio.h>voidmain(){inta[21],i,ran,max,min,sum,average;clrscr();人人好公,則天下太平;人人營(yíng)私,則天下大亂。——?jiǎng)Ⅸ?/p>

以家為家,以鄉(xiāng)為鄉(xiāng),以國(guó)為國(guó),以天下為天下?!豆茏印つ撩瘛穎or(i=1;i<=20;i++){while((ran=random(51))/10==0)//ensureranbetween20and50;a[i]=ran;}max=min=a[1];//initializeheresum=0;for(i=1;i<=20;i++){printf("a[%d]=%d\n",i,a[i]);sum+=a[i];if(max<a[i])max=a[i];elseif(min>a[i])min=a[i];}average=sum/20;printf("\nsum=%d,max=%d,min=%d,average=%d\n",sum,max,min,average);puts("\nanykeytoexit!");getche();}5、編程在一個(gè)已知的字符串中查找最長(zhǎng)單詞,假定字符串中只含字母和空格,空格用來(lái)分隔不同單詞。#include<stdio.h>#include<conio.h>voidmain(){charstring[80],*p;inti=0,max=0;clrscr();printf("pleaseinputastring:\n");gets(string);//printf("\n%s\n",string);寵辱不驚,看庭前花開(kāi)花落;去留無(wú)意,望天上云卷云舒?!逗閼?yīng)明》

丈夫志四方,有事先懸弧,焉能鈞三江,終年守菰蒲?!额櫻孜洹穚=string;while(*p!='\0'){if(*p==''){if(max<=i)max=i;i=0;}elsei++;p++;}if(max<=i)max=i;printf("\nmax_lengthofthestringis:%d\n",max);getche();}6、模擬n個(gè)人參加選舉的過(guò)程,并輸出選舉結(jié)果:假設(shè)候選人有四人,分別用A、B、C、D表示,當(dāng)選某候選人時(shí)直接輸入其編號(hào)(編號(hào)由計(jì)算機(jī)隨機(jī)產(chǎn)生),若輸入的不是A、B、C、D則視為無(wú)效票,選舉結(jié)束后按得票數(shù)從高到低輸出候選人編號(hào)和所得票數(shù)。#include<stdio.h>#include<conio.h>#include<stdlib.h>#include<time.h>voidmain(){inti,n,A,B,C,D,abandon,ran;//abandonstandfortheoneswhoabandonclrscr();A=B=C=D=abandon=0;printf("Enterthenumberofpersons:");scanf("%d",&n);//npersonstoselectfromcandidatea,b,candd.百學(xué)須先立志?!祆?/p>

好學(xué)近乎知,力行近乎仁,知恥近乎勇。——《中庸》randomize();for(i=1;i<=n;i++){ran=random(5)+65;//A,B,C,Dandtheoneswhoabandonswitch(ran){case'A':A++;break;case'B':B++;break;case'C':C++;break;case'D':D++;break;default:abandon++;}}printf("\nA=%d,B=%d,C=%d,D=%d,abandon=%d\n\n",A,B,C,D,abandon);i=4;while(i--!=0){if(A>=B&&A>=C&&A>=D&&A!=-1){printf("A=%d,",A);A=-1;}if(B>=A&&B>=C&&B>=D&&B!=-1){printf("B=%d,",B);B=-1;}if(C>=A&&C>=B&&C>=D&&C!=-1){printf("C=%d,",C);C=-1;}if(D>=A&&D>=B&&D>=C&&D!=-1){窮則獨(dú)善其身,達(dá)則兼善天下。——《孟子》

老當(dāng)益壯,寧移白首之心;窮且益堅(jiān),不墜青云之志?!啤ね醪猵rintf("D=%d,",D);D=-1;}}printf("abandon=%d,\n\n",abandon);getch();}7、任何一個(gè)自然數(shù)m的立方均可寫(xiě)成m個(gè)連續(xù)奇數(shù)之和。例如:1^3=12^3=3+53^3=7+9+114^3=13+15+17+19編程實(shí)現(xiàn):輸入一自然數(shù)n,求組成n3的n個(gè)連續(xù)奇數(shù)。#include<conio.h>#include<stdio.h>#include<math.h>//voidmain(){inti,j,n,sum,count;clrscr();printf("\nanintegerhereplease:");scanf("%d",&n);printf("\n");//i=(int)floor(sqrt((float)n));//if(i%2==0)//i++;i=1;//for(;i<=n*n*n;i+=2){sum=0;count=0;for(j=i;j<=n*n*n;j+=2){sum=sum+j;常將有日思無(wú)日,莫待無(wú)時(shí)思有時(shí)?!对鰪V賢文》

百川東到海,何時(shí)復(fù)西歸?少壯不盡力,老大徒傷悲?!獫h樂(lè)府《長(zhǎng)歌行》count++;if((n*n*n==sum)&&(count==n))break;}if((sum==n*n*n)&&(count==n))break;}printf("i=%d,j=%d\n\n",i,j);printf("%d*%d*%d=",n,n,n);//while(n-->1){printf("%d+",i);i+=2;}for(;i<j;i+=2)//printf("%d+",i);//printf("%d",i);getch();}8、已知abc+cba=1333,其中a,b,c均為一位數(shù),編程求出滿足條件的a,b,c所有組合。#include<stdio.h>#include<conio.h>voidmain(){intnum,a,b,c;clrscr();printf("Pressanykeytocalculate!\n\n");getch();for(num=101;num<1000;num++){a=num/100;b=num%100/10;c=num%10;if(num+c*100+b*10+a==1333)printf("\n%d+%d=1333\n",num,c*100+b*10+a);}while(!kbhit())老當(dāng)益壯,寧移白首之心;窮且益堅(jiān),不墜青云之志?!啤ね醪?/p>

人不知而不慍,不亦君子乎?——《論語(yǔ)》;}9、編制一個(gè)完成兩個(gè)數(shù)的四則運(yùn)算程序。如:用戶(hù)輸入34+56則輸出結(jié)果90.00。要求運(yùn)算結(jié)果保留兩位小數(shù),用戶(hù)輸入時(shí)一次將兩個(gè)數(shù)和操作符輸入。#include<stdio.h>#include<conio.h>voidmain(){floatnum1,num2;chars;clrscr();printf("Enterastatementplease:");scanf("%f%c%f",&num1,&s,&num2);switch(s){case'+':printf("%.2f%c%.2f=%.2f",num1,s,num2,num1+num2);break;case'-':printf("%.2f%c%.2f=%.2f",num1,s,num2,num1-num2);break;case'*':printf("%.2f%c%.2f=%.2f",num1,s,num2,num1*num2);break;case'/':printf("%.2f%c%.2f=%.2f",num1,s,num2,num1/num2);break;default:printf("InputError!");}getch();}10、輸入一個(gè)五位以?xún)?nèi)的正整數(shù),(1)判斷它是一個(gè)幾位數(shù);(2)請(qǐng)按序輸出其各位數(shù)字;(3)逆序輸出其各位數(shù)字。如輸入:56439,輸出:5位數(shù)5,6,4,3,99,3,4,6,5#include<conio.h>#include<stdio.h>longpower(intn)寵辱不驚,看庭前花開(kāi)花落;去留無(wú)意,望天上云卷云舒?!逗閼?yīng)明》

老當(dāng)益壯,寧移白首之心;窮且益堅(jiān),不墜青云之志。——唐·王勃{inti;longresult=1;for(i=1;i<=n;i++)result=10*result;returnresult;}voidmain(){longnum,n,j;intcount=1,tmpcount;clrscr();printf("Pleaseinputanumber(0-99999):");scanf("%ld",&num);n=num;while((n=n/10)!=0)count++;printf("\n%ldisa%ddigits.\n\n",num,count);tmpcount=count;//temporarytmpcountforlateruseinstatementAn=num;while(count-->1){j=power(count);printf("%d,",n/j);n=n%j;}printf("%d\n",n);n=num;while(tmpcount-->1)//statementA{printf("%d,",n%10);n=n/10;}丹青不知老將至,貧賤于我如浮云?!鸥?/p>

海納百川,有容乃大;壁立千仞,無(wú)欲則剛?!謩t徐printf("%d\n",n);getch();}11、編寫(xiě)子函數(shù):(1)用冒泡法將一個(gè)數(shù)組排成升序的函數(shù)---SUB1;(2)在升序數(shù)組中插入一個(gè)數(shù),并且保持該數(shù)組仍為升序數(shù)組的函數(shù)---SUB2。主函數(shù):①輸入任意10個(gè)正整數(shù)給數(shù)組;②調(diào)用SUB1對(duì)數(shù)組進(jìn)行排序;③從鍵盤(pán)輸入一個(gè)正整數(shù),調(diào)用SUB2將其插入該數(shù)組。#include<conio.h>#include<stdio.h>voidmain(){inti,k,a[12]={0};//a[0]fornousevoidsub1(intb[]),sub2(intb[],intk);clrscr();printf("Pleaseinput10numbers:");for(i=1;i<=10;i++)scanf("%d",&a[i]);getchar();sub1(a);for(i=1;i<=10;i++)printf("\na[%d]=%d\n",i,a[i]);printf("\n\npleaseinputanumbertobeinsertedintothearray:");scanf("%d",&k);sub2(a,k);for(i=1;i<=11;i++)printf("\na[%d]=%d\n",i,a[i]);puts("\nAnykeytoexit!");getch();}吾日三省乎吾身。為人謀而不忠乎?與朋友交而不信乎?傳不習(xí)乎?——《論語(yǔ)》

非淡泊無(wú)以明志,非寧?kù)o無(wú)以致遠(yuǎn)?!T葛亮voidsub1(b)intb[];{inti,j,t;for(i=1;i<10;i++)//thefirstoneisalwaysthesmallestfor(j=i;j<=10;j++)if(b[i]>b[j]){t=b[i];b[i]=b[j];b[j]=t;}}voidsub2(intb[],intk){inti;for(i=10;i>=1;i--){if(k<b[i])b[i+1]=b[i];else{b[i+1]=k;break;}}}12、編寫(xiě)函數(shù):(1)用選擇法將數(shù)組排成降序的函數(shù)----SUB1;(2)用折半查找法查找某數(shù)是否在給定的數(shù)組當(dāng)中的函數(shù)----SUB2。主函數(shù):輸入任意10個(gè)正整數(shù)給數(shù)組,調(diào)用SUB1對(duì)數(shù)組進(jìn)行排序,從鍵盤(pán)輸入一個(gè)正整數(shù),調(diào)用SUB2在數(shù)組中進(jìn)行查找,找到后輸出“OK”,沒(méi)有找到則輸出“NOFOUND!”。#include<conio.h>#include<stdio.h>以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失。——《舊唐書(shū)·魏征列傳》

寵辱不驚,看庭前花開(kāi)花落;去留無(wú)意,望天上云卷云舒?!逗閼?yīng)明》voidmain(){inti,key,a[11]={0},sub1(),sub2();printf("pleaseinput10number:");for(i=1;i<=10;i++)scanf("%d",&a[i]);getchar();sub1(a);for(i=0;i<=10;i++)printf("a[%d]=%d,",i,a[i]);printf("\npleaseinputakeynumber:");scanf("%d",&key);sub2(a,key,1,10);getch();}intsub1(intb[]){intt,i,j,post;for(i=1;i<10;i++){post=i;for(j=i+1;j<=10;j++)if(b[post]>b[j])post=j;if(post!=i){t=b[i];b[i]=b[post];b[post]=t;}}return0;}intsub2(intc[],intk,intn0,intn1){人人好公,則天下太平;人人營(yíng)私,則天下大亂?!?jiǎng)Ⅸ?/p>

吾日三省乎吾身。為人謀而不忠乎?與朋友交而不信乎?傳不習(xí)乎?——《論語(yǔ)》inti=n0,j=n1,m;m=(i+j)/2;while(i<=j){if(k<c[m])j=m-1;if(k>c[m])i=m+1;if(k==c[m])break;m=(i+j)/2;}if(k==c[m])printf("OK!\n");elseprintf("NOFOUND!\n");return0;}13、編寫(xiě)一個(gè)程序,輸入兩個(gè)包含5個(gè)元素的數(shù)組,先將兩個(gè)數(shù)組升序排列,然后將這兩個(gè)數(shù)組合并成一個(gè)升序數(shù)組。#include<stdio.h>#include<conio.h>voidmain(){inti,j,k,a[6]={0},b[6]={0},c[11]={0},sub1();clrscr();printf("\npleaseinput5intnumberstoarray1:");for(i=1;i<=5;i++)//a[0]fornousescanf("%d",&a[i]);getchar();sub1(a,5);printf("\npleaseinput5intnumberstoarray2:");for(i=1;i<=5;i++)//b[0]fornousescanf("%d",&b[i]);寵辱不驚,看庭前花開(kāi)花落;去留無(wú)意,望天上云卷云舒?!逗閼?yīng)明》

謀事在人,成事在天!——《增廣賢文》getchar();sub1(b,5);printf("\nthesortedarrayais:\n\n");for(i=1;i<=5;i++)printf("a[%d]=%d",i,a[i]);printf("\n");printf("\nthesortedarraybis:\n\n");for(i=1;i<=5;i++)printf("b[%d]=%d",i,b[i]);k=i=j=1;while(i<=5&&j<=5)if(a[i]<b[j])c[k++]=a[i++];//c[0]fornouseelsec[k++]=b[j++];if(i<j)//appendingtherestonesinarrayafor(;i<=5;i++)c[k++]=a[i];else//appendingtherestonesinarraybfor(;j<=5;j++)c[k++]=b[j];printf("\n\n");printf("\nthemergedarraycis:\n\n");for(k=1;k<=10;k++){if(k==6)printf("\n");printf("c[%d]=%d",k,c[k]);}while(!kbhit());}intsub1(intb[],intn){忍一句,息一怒,饒一著,退一步。——《增廣賢文》

窮則獨(dú)善其身,達(dá)則兼善天下?!睹献印穒ntt,i,j,post;for(i=1;i<n;i++){post=i;for(j=i+1;j<=n;j++)if(b[post]>b[j])post=j;if(post!=i){t=b[i];b[i]=b[post];b[post]=t;}}return0;}14、耶穌有13個(gè)門(mén)徒,其中有一個(gè)就是出賣(mài)耶穌的叛徒,請(qǐng)用排除法找出這位叛徒:13人圍坐一圈,從第一個(gè)開(kāi)始報(bào)號(hào):1,2,3,1,2,3……,凡是報(bào)到“3”就退出圈子,最后留在圈內(nèi)的人就是出賣(mài)耶穌的叛徒,請(qǐng)找出它原來(lái)的序號(hào)。/*//approachone#defineN13#include<stdio.h>#include<conio.h>structperson{intnumber;//itsorderintheoriginalcircleintnextp;//recorditsnextperson};structpersonlink[N+1];//link[0]fornousevoidmain(){inti,count,next;//countfor12persons,and以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失。——《舊唐書(shū)·魏征列傳》

勿以惡小而為之,勿以善小而不為?!?jiǎng)?/nextforthepersonnotoutofcircleyetclrscr();for(i=1;i<=N;i++){link[i].number=i;//numberingeachpersonif(i==N)link[i].nextp=1;elselink[i].nextp=i+1;//numberingeachnextperson}printf("\nThesequenceoutofthecircleis:\n");for(next=1,count=1;count<N;count++)//countuntil12persons{i=1;while(i!=3)//icounts1,2,3{do//skiptheoneswhosenumbersarezeronext=link[next].nextp;while(link[next].number==0);//endofdoi++;}printf("%3d",link[next].number);link[next].number=0;//indicateoutofcirclealreadydo//startfromtheoneswhosenumbersarenotzeronexttimenext=link[next].nextp;while(link[next].number==0);}printf("\n\nThebetrayerofthemis:");for(i=1;i<=N;i++)if(link[i].number)printf("%3d\n",link[i].number);getch();}*/古之立大事者,不惟有超世之才,亦必有堅(jiān)忍不拔之志。——蘇軾

人人好公,則天下太平;人人營(yíng)私,則天下大亂。——?jiǎng)Ⅸ?/approachtwousingcycliclist#defineN13#defineLENsizeof(structperson)#include<stdio.h>#include<conio.h>#include<alloc.h>#include<stdlib.h>//structperson//permitstructplacedhere////{//intnumber;//structperson*next;//};voidmain(){inti,count;structperson//orpermitstructplacedherealso//{intnumber;structperson*next;};structperson*head,*p1,*p2;clrscr();head=p2=NULL;for(i=1;i<=N;i++){p1=(structperson*)malloc(LEN);p1->number=i;if(head==NULL)head=p1;elsep2->next=p1;p2=p1;}常將有日思無(wú)日,莫待無(wú)時(shí)思有時(shí)?!对鰪V賢文》

先天下之憂而憂,后天下之樂(lè)而樂(lè)?!吨傺蚿2->next=head;printf("\nthesequenceoutofthecircleis:\n");for(count=1;count<N;count++){i=1;while(i!=3){p1=head;head=head->next;i++;}p2=head;printf("%3d",p2->number);p1->next=head=p2->next;free(p2);}printf("\nThebetrayerofthemis:\n%3d",head->number);getch();}15、編寫(xiě)一個(gè)程序,根據(jù)用戶(hù)輸入的不同邊長(zhǎng),輸出其菱形。例如,邊長(zhǎng)為3的菱形為:16、按如下圖形打印楊輝三角形的前10行。其特點(diǎn)是兩個(gè)腰上的數(shù)都為1,其它位置上的每一個(gè)數(shù)是它上一行相鄰兩個(gè)整數(shù)之和。#include<stdio.h>#include<conio.h>#defineN10voidmain(){inti,j,k,a[N][N];clrscr();天行健,君子以自強(qiáng)不息。地勢(shì)坤,君子以厚德載物?!兑捉?jīng)》

以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失?!杜f唐書(shū)·魏征列傳》for(i=0;i<N;i++)//initializea[N][N]{a[i][0]=1;a[i][i]=1;}for(i=2;i<N;i++)//calculatefor(j=1;j<i;j++)a[i][j]=a[i-1][j-1]+a[i-1][j];for(i=0;i<N;i++)//output{for(k=0;k<=3*(N-i);k++)printf("");for(j=0;j<=i;j++)printf("%6d",a[i][j]);printf("\n\n");}getch();}17、某班有5個(gè)學(xué)生,三門(mén)課。分別編寫(xiě)3個(gè)函數(shù)實(shí)現(xiàn)以下要求:(1)求各門(mén)課的平均分;(2)找出有兩門(mén)以上不及格的學(xué)生,并輸出其學(xué)號(hào)和不及格課程的成績(jī);(3)找出三門(mén)課平均成績(jī)?cè)?5-90分的學(xué)生,并輸出其學(xué)號(hào)和姓名主程序輸入5個(gè)學(xué)生的成績(jī),然后調(diào)用上述函數(shù)輸出結(jié)果。#defineSNUM5/*studentnumber*/#defineCNUM3/*coursenumber*/#include<stdio.h>#include<conio.h>/*dispstudentinfo*/voidDispScore(charnum[][6],charname[][20],floatscore[][CNUM]){inti,j;printf("\n\nStudentInfoandScore:\n");for(i=0;i<SNUM;i++)人之為學(xué),不日進(jìn)則日退,獨(dú)學(xué)無(wú)友,則孤陋而難成;久處一方,則習(xí)染而不自覺(jué)?!额櫻孜洹?/p>

一寸光陰一寸金,寸金難買(mǎi)寸光陰?!对鰪V賢文》{printf("%s",num[i]);printf("%s",name[i]);for(j=0;j<CNUM;j++)printf("%8.2f",score[i][j]);printf("\n\n");}}/*calculateallstudentaveragescore*/voidCalAver(floatscore[][CNUM]){floatsum,aver;inti,j;for(i=0;i<CNUM;i++){sum=0;for(j=0;j<SNUM;j++)sum=sum+score[j][i];aver=sum/SNUM;printf("Averagescoreofcourse%dis%8.2f\n",i+1,aver);}}/*Findstudent:twocoursesnopass*/voidFindNoPass(charnum[][6],floatscore[][CNUM]){inti,j,n;printf("\nTwoCourseNoPassStudents:\n");for(i=0;i<SNUM;i++){n=0;for(j=0;j<CNUM;j++)if(score[i][j]<60)n++;if(n>=2)我盡一杯,與君發(fā)三愿:一愿世清平,二愿身強(qiáng)健,三愿臨老頭,數(shù)與君相見(jiàn)?!栋拙右住?/p>

謀事在人,成事在天!——《增廣賢文》{printf("%s",num[i]);for(j=0;j<CNUM;j++)if(score[i][j]<60)printf("%8.2f",score[i][j]);printf("\n");}}}/*Findstudent:threecourses85-90*/voidFindGoodStud(charnum[][6],charname[][20],floatscore[][CNUM]){inti,j,n;printf("\nScoreofthreecoursesbetween85and90:\n");for(i=0;i<SNUM;i++){n=0;for(j=0;j<CNUM;j++)if(score[i][j]>=85&&score[i][j]<=90)n++;if(n==3)printf("%s%s\n",num[i],name[i]);}}/*inputstudentinfo*/voidmain(){charnum[SNUM][6],name[SNUM][20];//arraynumreferstostudentnumberfloatscore[SNUM][CNUM];//anditslengthis6inti,j;clrscr();printf("\nPleaseinputstudentnumandscore:\n");for(i=0;i<SNUM;i++)丹青不知老將至,貧賤于我如浮云。——杜甫

忍一句,息一怒,饒一著,退一步?!对鰪V賢文》{printf("\n\nStudent%dnumber:",i+1);scanf("%s",num[i]);printf("\nStudent%dname:",i+1);scanf("%s",name[i]);printf("\nStudent%dthreescores:",i+1);for(j=0;j<CNUM;j++)scanf("%f",&score[i][j]);}DispScore(num,name,score);CalAver(score);FindNoPass(num,score);FindGoodStud(num,name,score);getch();}18、編寫(xiě)一人個(gè)求X的Y次冪的遞歸函數(shù),X為double型,y為int型,要求從主函數(shù)輸入x,y的值,調(diào)用函數(shù)求其冪。#include<stdio.h>#include<conio.h>doublefact(doublex,inty){if(y==1)returnx;elsereturnx*fact(x,y-1);}voidmain(){doublex;inty;clrscr();printf("\nPleasex,y:");scanf("%lf%d",&x,&y);忍一句,息一怒,饒一著,退一步?!对鰪V賢文》

我盡一杯,與君發(fā)三愿:一愿世清平,二愿身強(qiáng)健,三愿臨老頭,數(shù)與君相見(jiàn)?!栋拙右住穚rintf("\nx^y=%.2lf",fact(x,y));getch();}19、打印魔方陣。所謂魔方陣是指這樣的的方陣:它的每一行、每一列和對(duì)角線之和均相等。輸入n,要求打印由自然數(shù)1到n2的自然數(shù)構(gòu)成的魔方陣(n為奇數(shù))。例如,當(dāng)n=3時(shí),魔方陣為:816357492魔方陣中各數(shù)排列規(guī)律為:①將“1”放在第一行的中②從“2”開(kāi)始直到n×n為止的各數(shù)同樣加1;間一列;依次按下列規(guī)則存放:每一個(gè)數(shù)存放的行比前一個(gè)數(shù)的行數(shù)減1,列數(shù)③如果上一數(shù)的行數(shù)為1,則下一個(gè)數(shù)的行數(shù)為n(最下一行),如在3×3方陣中,1在第1行,則2應(yīng)放在第3行第3列。④當(dāng)上一個(gè)數(shù)的列數(shù)為1,行數(shù)減1。如⑤如果按上面規(guī)則把下一個(gè)數(shù)放在上一個(gè)數(shù)的下面。如按上面的規(guī)定,4應(yīng)放在第1行第2列,但該位置已被1占據(jù),所以4就放在3的下面。由于6是第1行第3列n時(shí),下一個(gè)數(shù)的列數(shù)應(yīng)為2在第3行第3列,3應(yīng)在第2行第1列。則確定的位置上已有數(shù),或上一個(gè)數(shù)是第1行第n列時(shí),(即最后一列),故7放在6下面。#include<stdio.h>#include<conio.h>#defineMax15voidmain(){inti,row,col,odd;intm[Max][Max];clrscr();printf("\nPleaseinputanodd:");scanf("%d",&odd);if(odd<=0||odd%2==0){人之為學(xué),不日進(jìn)則日退,獨(dú)學(xué)無(wú)友,則孤陋而難成;久處一方,則習(xí)染而不自覺(jué)。——《顧炎武》

萬(wàn)兩黃金容易得,知心一個(gè)也難求?!恫苎┣邸穚rintf("\nInputError!\n");getch();return0;}printf("\nodd=%d\n\n",odd);row=0;col=odd/2;//1placedinthemiddleofthefirstrowfor(i=1;i<=odd*odd;i++){m[row][col]=i;if(i%odd==0)//tothelastcolif(row==odd-1)//tothelastrowrow=0;elserow++;else//outmostelse{if(row==0)row=odd-1;elserow--;if(col==odd-1)col=0;elsecol++;}//endofoutmostelse}//endofforfor(row=0;row<odd;row++){for(col=0;col<odd;col++)printf("%4d",m[row][col]);printf("\n\n");}getch();return0;常將有日思無(wú)日,莫待無(wú)時(shí)思有時(shí)?!对鰪V賢文》

吾日三省乎吾身。為人謀而不忠乎?與朋友交而不信乎?傳不習(xí)乎?——《論語(yǔ)》}20、找出一個(gè)二維數(shù)組中的“鞍點(diǎn)”,即該位置上的元素在該行中最大,在該列中最?。ㄒ部赡軟](méi)有“鞍點(diǎn)”),打印出有關(guān)信息。#defineN20#defineM20#include<stdio.h>#include<conio.h>voidmain(){inta[N][M];//inta[][];notallowedhereinti,j,k,row,col,n,m,find=0;clrscr();printf("\nEntern&m:\n\n");scanf("%d%d",&n,&m);printf("\nEntera[0][0]--a[%d][%d]\n\n",n-1,m-1);for(i=0;i<n;i++)for(j=0;j<m;j++)scanf("%d",&a[i][j]);printf("\n\nThearrayyouhavejustenteredis:\n");for(i=0;i<n;i++){for(j=0;j<m;j++)printf("%5d",a[i][j]);printf("\n\n");}//findthepointfor(i=0;i<n;i++){for(col=0,j=1;j<m;j++)if(a[i][col]<a[i][j])//findcol,selectsortaccordingtocolcol=j;for(row=0,k=1;k<n;k++)if(a[row][col]>a[k][col])//findrow,selectsortaccordingtorow寵辱不驚,看庭前花開(kāi)花落;去留無(wú)意,望天上云卷云舒?!逗閼?yīng)明》

勿以惡小而為之,勿以善小而不為?!?jiǎng)鋜ow=k;if(i==row){find=1;printf("Thepointisa[%d][%d].\n",row,col);}}if(!find)printf("\nNosolution.\n");getch();}21、馬克思在《數(shù)學(xué)手稿》中提出如下問(wèn)題:有30個(gè)人(包括男人、女人和小孩)在一家飯店吃飯共花50先令,其中每個(gè)男人花3先令,每個(gè)女人花2先令,每個(gè)小孩花1先令,問(wèn)男人、女人、小孩各有多少人?#include<stdio.h>#include<conio.h>voidmain(){intman,woman,child,money=50,count=30;inti,j,k;clrscr();printf("Theyconsistof:\n\n");for(i=0;i<=count;i++)for(j=0;j<=count;j++)for(k=0;k<=count;k++)if(i+j+k==count&&3*i+2*j+k==money){printf("man=%2dwoman=%2dchild=%2d\n",i,j,k);printf("\n");}getch();}謀事在人,成事在天!——《增廣賢文》

百學(xué)須先立志?!祆?2、定義一個(gè)結(jié)構(gòu)體變量(包括年、月、日),計(jì)算該日在本年中為第幾天?(注意考慮閏年問(wèn)題),要求寫(xiě)一個(gè)函數(shù)days,實(shí)現(xiàn)上面的計(jì)算。由主函數(shù)將年月日傳遞給days函數(shù),計(jì)算后將日子傳遞回主函數(shù)輸出。#include<stdio.h>#include<conio.h>structymd{intday;intmonth;intyear;};intdayof[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};intdays(structymd*p){inti,d;if(p->year%4==0&&p->year%100!=0||p->year%400==0)dayof[2]=29;d=p->day;for(i=1;i<p->month;i++)d=d+dayof[i];return(d);}voidmain(){structymddate;intd;clrscr();for(;;){printf("\n-----------------------------------\n\n");printf("date(yyyy/mm/dd)=?(yyyy=0--Exit)\n\n");scanf("%d/%d/%d",&date.year,&date.month,&date.day);if(date.year==0)人不知而不慍,不亦君子乎?——《論語(yǔ)》

先天下之憂而憂,后天下之樂(lè)而樂(lè)?!吨傺蚥reak;d=days(&date);printf("\nThedayoftheyearis%d!\n\n",d);}}23、建立一個(gè)鏈表,每個(gè)結(jié)點(diǎn)包括:學(xué)號(hào)、姓名、性別、年齡,輸入一個(gè)學(xué)號(hào),如果鏈表中的結(jié)點(diǎn)包括該學(xué)號(hào),則輸出該結(jié)點(diǎn)內(nèi)容后,并將其結(jié)點(diǎn)刪去。#defineLENsizeof(structstud_node)#include<conio.h>#include<stdio.h>#include<string.h>structstud_record{charStudNo[6];charStudName[10];charStudSex;/*M---MaleF---Female*/intStudAge;};structstud_node{structstud_recordstud_mem;structstud_node*next;};/*CreateStudentLineartable*/structstud_node*create(){structstud_node*head,*p,*q;charvno[6],vname[10],vsex;intvage;head=NULL;while(1){printf("\nPleaseinputastudentrecord\n\nNo\tName\tSex\tAge\n\n");勿以惡小而為之,勿以善小而不為?!?jiǎng)?/p>

丹青不知老將至,貧賤于我如浮云?!鸥canf("%s",vno);getchar();if(strcmp(vno,"0")==0)//whenvno=="0"toexitbreak;scanf("%s",vname);getchar();scanf("%c%d",&vsex,&vage);getchar();p=(structstud_node*)malloc(LEN);//allocatespacetonodepstrcpy(p->stud_mem.StudNo,vno);strcpy(p->stud_mem.StudName,vname);p->stud_mem.StudSex=vsex;p->stud_mem.StudAge=vage;if(head==NULL)head=p;elseq->next=p;//qisthepreviousnodeofpq=p;}if(head!=NULL)q->next=NULL;//thelastnodehasnochildreturnhead;}/*FindastudentandIfFoundthenDeletethenode*/structstud_node*delete(structstud_node*head,charno[6]){structstud_node*p,*q;p=head;q=p;while(p){if(strcmp(p->stud_mem.StudNo,no)==0)/*Deletethenode*/{if(p==head)//deletethefirstnodehead=p->next;萬(wàn)兩黃金容易得,知心一個(gè)也難求?!恫苎┣邸?/p>

我盡一杯,與君發(fā)三愿:一愿世清平,二愿身強(qiáng)健,三愿臨老頭,數(shù)與君相見(jiàn)?!栋拙右住積lse{if(p->next!=NULL)//deletethemiddlenodeq->next=p->next;else//deletethelastnodeq->next=NULL;}printf("\n\t\t%s\t%s\t%c\t%d\n",p->stud_mem.StudNo,p->stud_mem.StudName,p->stud_mem.StudSex,p->stud_mem.StudAge);free(p);break;}q=p;p=p->next;}returnhead;}/*Displineartablecontent*/voidprn(structstud_node*head){structstud_node*p;inti=1;p=head;printf("\nRecord\tNo\tName\tSex\tAge\n");while(p){printf("%3d\t%s\t%s\t%c\t%d\n",i,p->stud_mem.StudNo,p->stud_mem.StudName,p->stud_mem.StudSex,p->stud_mem.StudAge);p=p->next;i++;}}/*mainprogramhere*/voidmain()非淡泊無(wú)以明志,非寧?kù)o無(wú)以致遠(yuǎn)?!T葛亮

良辰美景奈何天,便賞心樂(lè)事誰(shuí)家院。則為你如花美眷,似水流年。——《湯顯祖》{structstud_node*head;charno[6];clrscr();head=create();prn(head);getch();printf("\nPleaseinputastudnotoFind:");gets(no);head=delete(head,no);prn(head);getch();}24、給定一個(gè)日期,求出該日為星期幾(已知2002-3-28為星期四)。#include<stdio.h>#include<conio.h>structymd{intyear;intmonth;intday;};/*ifayearisaleapone*/intyn_rn(intyear){if(year%4==0&&year%100!=0||year%400==0)return1;elsereturn0;}/*returnwhichdayintheyear*/intd_of_day(structymddayof)良辰美景奈何天,便賞心樂(lè)事誰(shuí)家院。則為你如花美眷,似水流年?!稖@祖》

好學(xué)近乎知,力行近乎仁,知恥近乎勇?!吨杏埂穥intdays[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};inti,d=0;if(yn_rn(dayof.year))days[2]=29;for(i=1;i<dayof.month;i++)d=d+days[i];return(d+dayof.day);}/*returnthepositivedaysifday1>day2,ornegativedaysifday1<day2*/intday_diff(structymdday1,structymdday2){intd1,d2,i,diff=0;d1=d_of_day(day1);d2=d_of_day(day2);if(day1.year>day2.year){for(i=day2.year;i<day1.year;i++)if(yn_rn(i))diff=diff+366;elsediff=diff+365;}else{for(i=day1.year;i<day2.year;i++)if(yn_rn(i))diff=diff-366;elsediff=diff-365;}returndiff+d1-d2;}voidmain()其身正,不令而行;其身不正,雖令不從?!墩撜Z(yǔ)》

以家為家,以鄉(xiāng)為鄉(xiāng),以國(guó)為國(guó),以天下為天下?!豆茏印つ撩瘛穥structymdoldday,day;intoldweek,week,diff;char*rq[7]={"Sun","Mon","Tue","Wen","Thu","Fri","Sat"};clrscr();/*2003-4-3:Thursday*/oldday.year=2003;oldday.month=4;oldday.day=3;oldweek=4;printf("\nPleaseinputday(YYYY-MM-DD):");scanf("%d-%d-%d",&day.year,&day.month,&day.day);diff=day_diff(day,oldday);week=(diff%7+oldweek)%7;printf("\n%d*%d-%d:%s\n",day.year,day.month,day.day,rq[week]);getch();}25、用遞歸法將一個(gè)整數(shù)n轉(zhuǎn)換成字符串(例如輸入4679,應(yīng)輸出字符串“4679”),n為不確定數(shù),可以是位數(shù)不超過(guò)5,且數(shù)值在-32768~32767之間和任意整數(shù)。#include<conio.h>#include<stdio.h>voidconvert(intn){inti;if((i=n/10)!=0)convert(i);putchar(n%10+'0');//theAsciiofchar'0'is48}voidmain(){intnumber;clrscr();常將有日思無(wú)日,莫待無(wú)時(shí)思有時(shí)。——《增廣賢文》

以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失?!杜f唐書(shū)·魏征列傳》printf("\ninputaninteger:");scanf("%d",&number);printf("\n\nOutputstringis:");if(number<0){putchar('-');number=-number;}convert(number);getch();}26、有一個(gè)字符串,包括n個(gè)字符。寫(xiě)一個(gè)函數(shù),將此字符串從第m個(gè)字符開(kāi)始的全部字符復(fù)制成另一個(gè)字符串。要求在主函數(shù)輸入字符串及m值并輸出復(fù)制結(jié)果。#include<stdio.h>#include<ctype.h>main(){intm;char*str1[80],*str2[80];printf("Inputastring(length<80:");scanf("%s",str1);printf("\nWhichcharacterstartingfrom?");scanf("%d",&m);if(strlen(str1)<m)printf("\nErrorinput!");else{copystr(str1,str2,m);printf("\nResultis:%s",str2);}}copystr(p1,p2,m)char*p1,*p2;intm;{intn;n=0;窮則獨(dú)善其身,達(dá)則兼善天下。——《孟

溫馨提示

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

最新文檔

評(píng)論

0/150

提交評(píng)論