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

下載本文檔

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

文檔簡介

謀事在人,成事在天!——《增廣賢文》C語言編程經(jīng)典及參考答案1、編程實現(xiàn)對鍵盤輸入的英文名句子進行加密。用加密方法為,當內容為英文字母時其在26字母中的其后三個字母代替該字母,若為其它字符時不變:#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、編程實現(xiàn)將任意的十進制整數(shù)轉換成R進制數(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;}人人好公,則天下太平;人人營私,則天下大亂?!獎Ⅸ?/p>

人不知而不慍,不亦君子乎?——《論語》while(n!=0){a[i++]=n%R;n=n/R;}while(i-->0)printf("%c",str[a[i]]);getche();}3、從鍵盤輸入一指定金額(以元為單位,如345.78),然后顯示支付該金額的各種面額人民幣數(shù)量,要求顯示100元、50元、10元、5元、2元、1元、1角、5分、1分各多少張。#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++;一寸光陰一寸金,寸金難買寸光陰?!对鰪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、隨機產(chǎn)生20個[10,50]的正整數(shù)存放到數(shù)組中,并求數(shù)組中的所有元素最大值、最小值、平均值及各元素之和。#include<stdlib.h>#include<conio.h>voidmain(){inta[21],i,ran,max,min,sum,average;clrscr();人人好公,則天下太平;人人營私,則天下大亂?!獎Ⅸ?/p>

以家為家,以鄉(xiāng)為鄉(xiāng),以國為國,以天下為天下?!豆茏印つ撩瘛穎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、編程在一個已知的字符串中查找最長單詞,假定字符串中只含字母和空格,空格用來分隔不同單詞。#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);寵辱不驚,看庭前花開花落;去留無意,望天上云卷云舒?!逗閼鳌?/p>

丈夫志四方,有事先懸弧,焉能鈞三江,終年守菰蒲?!额櫻孜洹穚=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個人參加選舉的過程,并輸出選舉結果:假設候選人有四人,分別用A、B、C、D表示,當選某候選人時直接輸入其編號(編號由計算機隨機產(chǎn)生),若輸入的不是A、B、C、D則視為無效票,選舉結束后按得票數(shù)從高到低輸出候選人編號和所得票數(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.百學須先立志?!祆?/p>

好學近乎知,力行近乎仁,知恥近乎勇。——《中庸》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){窮則獨善其身,達則兼善天下?!睹献印?/p>

老當益壯,寧移白首之心;窮且益堅,不墜青云之志?!啤ね醪猵rintf("D=%d,",D);D=-1;}}printf("abandon=%d,\n\n",abandon);getch();}7、任何一個自然數(shù)m的立方均可寫成m個連續(xù)奇數(shù)之和。例如:1^3=12^3=3+53^3=7+9+114^3=13+15+17+19編程實現(xiàn):輸入一自然數(shù)n,求組成n3的n個連續(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;常將有日思無日,莫待無時思有時?!对鰪V賢文》

百川東到海,何時復西歸?少壯不盡力,老大徒傷悲。——漢樂府《長歌行》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())老當益壯,寧移白首之心;窮且益堅,不墜青云之志?!啤ね醪?/p>

人不知而不慍,不亦君子乎?——《論語》;}9、編制一個完成兩個數(shù)的四則運算程序。如:用戶輸入34+56則輸出結果90.00。要求運算結果保留兩位小數(shù),用戶輸入時一次將兩個數(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、輸入一個五位以內的正整數(shù),(1)判斷它是一個幾位數(shù);(2)請按序輸出其各位數(shù)字;(3)逆序輸出其各位數(shù)字。如輸入:56439,輸出:5位數(shù)5,6,4,3,99,3,4,6,5#include<conio.h>#include<stdio.h>longpower(intn)寵辱不驚,看庭前花開花落;去留無意,望天上云卷云舒?!逗閼鳌?/p>

老當益壯,寧移白首之心;窮且益堅,不墜青云之志。——唐·王勃{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>

海納百川,有容乃大;壁立千仞,無欲則剛?!謩t徐printf("%d\n",n);getch();}11、編寫子函數(shù):(1)用冒泡法將一個數(shù)組排成升序的函數(shù)---SUB1;(2)在升序數(shù)組中插入一個數(shù),并且保持該數(shù)組仍為升序數(shù)組的函數(shù)---SUB2。主函數(shù):①輸入任意10個正整數(shù)給數(shù)組;②調用SUB1對數(shù)組進行排序;③從鍵盤輸入一個正整數(shù),調用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();}吾日三省乎吾身。為人謀而不忠乎?與朋友交而不信乎?傳不習乎?——《論語》

非淡泊無以明志,非寧靜無以致遠?!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、編寫函數(shù):(1)用選擇法將數(shù)組排成降序的函數(shù)----SUB1;(2)用折半查找法查找某數(shù)是否在給定的數(shù)組當中的函數(shù)----SUB2。主函數(shù):輸入任意10個正整數(shù)給數(shù)組,調用SUB1對數(shù)組進行排序,從鍵盤輸入一個正整數(shù),調用SUB2在數(shù)組中進行查找,找到后輸出“OK”,沒有找到則輸出“NOFOUND!”。#include<conio.h>#include<stdio.h>以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失?!杜f唐書·魏征列傳》

寵辱不驚,看庭前花開花落;去留無意,望天上云卷云舒。——《洪應明》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){人人好公,則天下太平;人人營私,則天下大亂?!獎Ⅸ?/p>

吾日三省乎吾身。為人謀而不忠乎?與朋友交而不信乎?傳不習乎?——《論語》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、編寫一個程序,輸入兩個包含5個元素的數(shù)組,先將兩個數(shù)組升序排列,然后將這兩個數(shù)組合并成一個升序數(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]);寵辱不驚,看庭前花開花落;去留無意,望天上云卷云舒。——《洪應明》

謀事在人,成事在天!——《增廣賢文》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){忍一句,息一怒,饒一著,退一步。——《增廣賢文》

窮則獨善其身,達則兼善天下?!睹献印穒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個門徒,其中有一個就是出賣耶穌的叛徒,請用排除法找出這位叛徒:13人圍坐一圈,從第一個開始報號:1,2,3,1,2,3……,凡是報到“3”就退出圈子,最后留在圈內的人就是出賣耶穌的叛徒,請找出它原來的序號。/*//approachone#defineN13#include<stdio.h>#include<conio.h>structperson{intnumber;//itsorderintheoriginalcircleintnextp;//recorditsnextperson};structpersonlink[N+1];//link[0]fornousevoidmain(){inti,count,next;//countfor12persons,and以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失。——《舊唐書·魏征列傳》

勿以惡小而為之,勿以善小而不為?!獎?/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();}*/古之立大事者,不惟有超世之才,亦必有堅忍不拔之志?!K軾

人人好公,則天下太平;人人營私,則天下大亂?!獎Ⅸ?/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;}常將有日思無日,莫待無時思有時?!对鰪V賢文》

先天下之憂而憂,后天下之樂而樂?!吨傺蚿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、編寫一個程序,根據(jù)用戶輸入的不同邊長,輸出其菱形。例如,邊長為3的菱形為:16、按如下圖形打印楊輝三角形的前10行。其特點是兩個腰上的數(shù)都為1,其它位置上的每一個數(shù)是它上一行相鄰兩個整數(shù)之和。#include<stdio.h>#include<conio.h>#defineN10voidmain(){inti,j,k,a[N][N];clrscr();天行健,君子以自強不息。地勢坤,君子以厚德載物?!兑捉?jīng)》

以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失。——《舊唐書·魏征列傳》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個學生,三門課。分別編寫3個函數(shù)實現(xiàn)以下要求:(1)求各門課的平均分;(2)找出有兩門以上不及格的學生,并輸出其學號和不及格課程的成績;(3)找出三門課平均成績在85-90分的學生,并輸出其學號和姓名主程序輸入5個學生的成績,然后調用上述函數(shù)輸出結果。#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++)人之為學,不日進則日退,獨學無友,則孤陋而難成;久處一方,則習染而不自覺?!额櫻孜洹?/p>

一寸光陰一寸金,寸金難買寸光陰?!对鰪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ā)三愿:一愿世清平,二愿身強健,三愿臨老頭,數(shù)與君相見?!栋拙右住?/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、編寫一人個求X的Y次冪的遞歸函數(shù),X為double型,y為int型,要求從主函數(shù)輸入x,y的值,調用函數(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ā)三愿:一愿世清平,二愿身強健,三愿臨老頭,數(shù)與君相見?!栋拙右住穚rintf("\nx^y=%.2lf",fact(x,y));getch();}19、打印魔方陣。所謂魔方陣是指這樣的的方陣:它的每一行、每一列和對角線之和均相等。輸入n,要求打印由自然數(shù)1到n2的自然數(shù)構成的魔方陣(n為奇數(shù))。例如,當n=3時,魔方陣為:816357492魔方陣中各數(shù)排列規(guī)律為:①將“1”放在第一行的中②從“2”開始直到n×n為止的各數(shù)同樣加1;間一列;依次按下列規(guī)則存放:每一個數(shù)存放的行比前一個數(shù)的行數(shù)減1,列數(shù)③如果上一數(shù)的行數(shù)為1,則下一個數(shù)的行數(shù)為n(最下一行),如在3×3方陣中,1在第1行,則2應放在第3行第3列。④當上一個數(shù)的列數(shù)為1,行數(shù)減1。如⑤如果按上面規(guī)則把下一個數(shù)放在上一個數(shù)的下面。如按上面的規(guī)定,4應放在第1行第2列,但該位置已被1占據(jù),所以4就放在3的下面。由于6是第1行第3列n時,下一個數(shù)的列數(shù)應為2在第3行第3列,3應在第2行第1列。則確定的位置上已有數(shù),或上一個數(shù)是第1行第n列時,(即最后一列),故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){人之為學,不日進則日退,獨學無友,則孤陋而難成;久處一方,則習染而不自覺?!额櫻孜洹?/p>

萬兩黃金容易得,知心一個也難求?!恫苎┣邸穚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;常將有日思無日,莫待無時思有時?!对鰪V賢文》

吾日三省乎吾身。為人謀而不忠乎?與朋友交而不信乎?傳不習乎?——《論語》}20、找出一個二維數(shù)組中的“鞍點”,即該位置上的元素在該行中最大,在該列中最?。ㄒ部赡軟]有“鞍點”),打印出有關信息。#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寵辱不驚,看庭前花開花落;去留無意,望天上云卷云舒。——《洪應明》

勿以惡小而為之,勿以善小而不為?!獎鋜ow=k;if(i==row){find=1;printf("Thepointisa[%d][%d].\n",row,col);}}if(!find)printf("\nNosolution.\n");getch();}21、馬克思在《數(shù)學手稿》中提出如下問題:有30個人(包括男人、女人和小孩)在一家飯店吃飯共花50先令,其中每個男人花3先令,每個女人花2先令,每個小孩花1先令,問男人、女人、小孩各有多少人?#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();}謀事在人,成事在天!——《增廣賢文》

百學須先立志?!祆?2、定義一個結構體變量(包括年、月、日),計算該日在本年中為第幾天?(注意考慮閏年問題),要求寫一個函數(shù)days,實現(xiàn)上面的計算。由主函數(shù)將年月日傳遞給days函數(shù),計算后將日子傳遞回主函數(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)人不知而不慍,不亦君子乎?——《論語》

先天下之憂而憂,后天下之樂而樂。——范仲淹break;d=days(&date);printf("\nThedayoftheyearis%d!\n\n",d);}}23、建立一個鏈表,每個結點包括:學號、姓名、性別、年齡,輸入一個學號,如果鏈表中的結點包括該學號,則輸出該結點內容后,并將其結點刪去。#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");勿以惡小而為之,勿以善小而不為?!獎?/p>

丹青不知老將至,貧賤于我如浮云。——杜甫scanf("%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;萬兩黃金容易得,知心一個也難求?!恫苎┣邸?/p>

我盡一杯,與君發(fā)三愿:一愿世清平,二愿身強健,三愿臨老頭,數(shù)與君相見。——《白居易》else{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()非淡泊無以明志,非寧靜無以致遠?!T葛亮

良辰美景奈何天,便賞心樂事誰家院。則為你如花美眷,似水流年?!稖@祖》{structstud_node*head;charno[6];clrscr();head=create();prn(head);getch();printf("\nPleaseinputastudnotoFind:");gets(no);head=delete(head,no);prn(head);getch();}24、給定一個日期,求出該日為星期幾(已知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)良辰美景奈何天,便賞心樂事誰家院。則為你如花美眷,似水流年?!稖@祖》

好學近乎知,力行近乎仁,知恥近乎勇?!吨杏埂穥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》

以家為家,以鄉(xiāng)為鄉(xiāng),以國為國,以天下為天下。——《管子·牧民》{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、用遞歸法將一個整數(shù)n轉換成字符串(例如輸入4679,應輸出字符串“4679”),n為不確定數(shù),可以是位數(shù)不超過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();常將有日思無日,莫待無時思有時。——《增廣賢文》

以銅為鏡,可以正衣冠;以古為鏡,可以知興替;以人為鏡,可以明得失?!杜f唐書·魏征列傳》printf("\ninputaninteger:");scanf("%d",&number);printf("\n\nOutputstringis:");if(number<0){putchar('-');number=-number;}convert(number);getch();}26、有一個字符串,包括n個字符。寫一個函數(shù),將此字符串從第m個字符開始的全部字符復制成另一個字符串。要求在主函數(shù)輸入字符串及m值并輸出復制結果。#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;窮則獨善其身,達則兼善天下。——《孟

溫馨提示

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

評論

0/150

提交評論