100道经典的C语言编程题及解答

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

1.输入两个正整数,m和n,求其最大公约数和最小公倍数。#includevoidmain(){inthcf(int,int);intlcd(int,int,int);intu,v,h,l;printf(Pleaseinputtwonumbers:\n);scanf(%d,%d,&u,&v);h=hcf(u,v);printf(H.C.F=%d\n,h);l=lcd(u,v,h);printf(L.C.D=%d\n,l);}inthcf(intu,intv){intt,r;if(vu){t=u;u=v;v=t;}while((r=u%v)!=0){u=v;v=r;}return(v);}intlcd(intu,intv,inth){return(u*v/h);}2.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#includeintletter,digit,space,others;voidmain(){voidcount(char[]);chartext[80];printf(Pleaseinputstring:\n);gets(text);printf(string:\n);puts(text);letter=0;digit=0;space=0;others=0;count(text);printf(letter:%d,digit:%d,space:%d,others:%d\n,letter,digit,space,others);}voidcount(charstr[]){inti;for(i=0;str!='\0';i++)if((str='a'&&str='z')||(str='A'&&str='Z'))letter++;elseif(str='0'&&str='9')digit++;elseif(str==32)space++;elseothers++;}3.输入一个正整数求出它是几位数;输出原数和位数。#includeintdigit;voidmain(){voidcount(char[]);chartext[80];printf(Pleaseinputnumbers:\n);gets(text);printf(Numbers:\n);puts(text);digit=0;count(text);printf(digit:%d\n,digit);}voidcount(charstr[]){inti;for(i=0;str!='\0';i++)if(str='0'&&str='9')digit++;}4.输入一个正整数,输出原数并逆序打印出各位数字。#includevoidinvertLongInt(long);voidmain(){unsignedlongiNumber;printf(Pleaseinputanumber:\n);scanf(%ld,&iNumber);printf(Theinputnumberis:%ld\n,iNumber);printf(Theinversenumberis:);invertLongInt(iNumber);}voidinvertLongInt(longx){if(x=0&&x=9)printf(%d\n,x);else{printf(%d,x%10);invertLongInt(x/10);}}5.从键盘上输入若干学生的一门课成绩,统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入。6.从键盘上输入若干学生的一门课成绩,计算出平均分,当输入负数时结束输入。将结果输出。7.求1!+2!+3!+……+20!,将结果输出。#includevoidmain(){floats=0,t=1;intn;for(n=1;n=20;n++){t=t*n;s=s+t;}printf(1!+2!+3!+……+20!=%e\n,s);}8.打印以下图案:****************#includevoidmain(){inti,j;printf(Thepictureis:\n);staticcharpicture[4][7]={{'','','','*'},{'','','*','*','*'},{'','*','*','*','*','*'},{'*','*','*','*','*','*','*'}};for(i=0;i=3;i++){for(j=0;j=6;j++)printf(%c,picture[j]);printf(\n);}}9.打印以下图案:**********#includevoidmain(){inti,j;printf(Thepictureis:\n);charpicture[4][4]={{'*'},{'*','*'},{'*','*','*'},{'*','*','*','*'}};for(i=0;i=3;i++){for(j=0;j=3;j++)printf(%c,picture[j]);printf(\n);}}10.求下列试子的值:1-1/2+1/3-1/4+……+1/99-1/100,将结果输出。#includevoidmain(){floatsum=1.0,t,s=1;inti;for(i=1;i=100;i++){t=s/i;sum=sum+t;s=-s;}printf(1-1/2+1/3-1/4+……+1/99-1/100=%5.4f\n,sum);}11.打印出100~999之间的所有水仙花数。#includevoidmain(){inti,j,k,n;printf(100~999之间的所有水仙花数are:\n);for(n=100;n1000;n++){i=n/100;j=n/10-i*10;k=n%10;if(n==i*i*i+j*j*j+k*k*k)printf(%d,n);}printf(\n);}12.求Sn=a+aa+aaa+…+aa…a之值,n,a由键盘输入。#includevoidmain(){inta,n,i=1,sn=0,tn=0;printf(a,n=:);scanf(%d,%d,&a,&n);while(i=n){tn=tn+a;sn=sn+tn;a=a*10;++i;}printf(a+aa+aaa+…+aa…a=%d\n,sn);}13.打印以下图案:****************************#includevoidmain(){chara[7]={'*','*','*','*','*','*','*'};inti,j,k;charspace='';printf(Thepictureis:);for(i=0;i4;i++){printf(\n);printf();for(j=1;j=i;j++)printf(%c,space);for(k=0;k7;k++)printf(%c,a[k]);}printf(\n);}14.打印以下图案:112112321123432115.打印以下图案:123432112321121116.编写一个统计学生成绩程序,完成以下功能:输入4个学生的2门课成绩;求出全班的总平均分,将结果输出。#include#defineN4structstudent{charnum[3];charname[4];floatscore[2];floatavr;}stu[N];voidmain(){inti,j;floatsum,average;for(i=0;i{printf(inputscoresofstudent%d:\n,i+1);printf(name:);scanf(%s,stu.name);for(j=0;j2;j++){printf(score%d:,j+1);scanf(%f,&stu.score[j]);}}average=0;for(i=0;i{sum=0;for(j=0;j2;j++)sum+=stu.score[j];stu.avr=sum/2;average+=stu.avr;}average/=N;printf(Namescore1score2average\n);for(i=0;i{printf(%5s%10s,stu.num,stu.name);for(j=0;j2;j++)printf(%9.2f,stu.score[j]);printf(%8.2f\n,stu.avr);}printf(average=%5.2f\n,average);}17.打印以下图案:*************************#includevoidmain(){chara[5]={'*','*','*','*','*'};inti,j,k;charspace='';printf(Thepictureis:);for(i=0;i5;i++){printf(\n);printf();for(j=1;j=i;j++)printf(%c,space);for(k=0;k5;k++)printf(%c,a[k]);}printf(\n);}18.给出年、月、日,计算该日是该年的第几天。#includevoidmain(){intsum_day(int,int);intleap(intyear);intyear,month,day,days;printf(inputdate(year,month,day):);scanf(%d,%d,%d,&year,&month,&day);printf(%d/%d/%d,year,month,day);days=sum_day(month,day);if(leap(year)&&month=3)days=days+1;printf(isthe%dthdayinthisyear.\n,days);}intsum_day(intmonth,intday){intday_tab[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};inti;for(i=1;iday+=day_tab;return(day);}intleap(intyear){intleap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}19.求一个3*3的整型矩阵对角线元素之和。将原矩阵和求出的和输出。#includevoidmain(){floata[3][3],sum=0;inti,j;printf(请输入元素:\n);for(i=0;i3;i++)for(j=0;j3;j++)scanf(%f,&a[j]);for(i=0;i3;i++)sum=sum+a;printf(对角线之和是:%6.2f\n,sum);for(i=0;i=3;i++){for(j=0;j=3;j++)printf(%5.2f,a[j]);printf(\n);}}20.求一个4*3的矩阵各行元素的平均值;将原矩阵和求出的平均值全部输出。#includevoidmain(){inta[4][3],s[4],i,j,k;for(i=0;i4;i++)for(j=0;j3;j++)scanf(%d,*(a+i)+j);for(i=0;i4;i++){k=0;for(j=0;j3;j++)k+=a[j];printf(第%d行的平均值是%d,i+1,k);printf(\n);}}21.求一个3*4的矩阵各列元素的平均值;将原矩阵和求出的平均值全部输出。#includevoidmain(){inta[4][3],s[4],i,j,k;for(i=0;i4;i++)for(j=0;j3;j++)scanf(%d,*(a+i)+j);for(i=0;i4;i++){k=0;for(j=0;j3;j++)k+=a[j];printf(第%d行的平均值是%d,i+1,k);printf(\n);}}22.求一

1 / 105
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功