您好,欢迎访问三七文档
c语言111道例题1.输入两个正整数,m和n,求其最大公约数和最小公倍数。#includestdio.hvoidmain(){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.输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#includestdio.hintletter,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[i]!='\0';i++)if((str[i]='a'&&str[i]='z')||(str[i]='A'&&str[i]='Z'))letter++;elseif(str[i]='0'&&str[i]='9')digit++;elseif(str[i]==32)space++;elseothers++;}3.输入一个正整数求出它是几位数;输出原数和位数。#includestdio.hintdigit;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[i]!='\0';i++)if(str[i]='0'&&str[i]='9')digit++;}4.输入一个正整数,输出原数并逆序打印出各位数字。#includestdio.hvoidinvertLongInt(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!,将结果输出。#includestdio.hvoidmain(){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.打印以下图案:****************#includestdio.hvoidmain(){inti,j;printf(Thepictureis:\n);staticcharpicture[4][7]={{'','','','*'},{'','','*','*','*'},{'','*','*','*','*','*'},{'*','*','*','*','*','*','*'}};for(i=0;i=3;i++){for(j=0;j=6;j++)printf(%c,picture[i][j]);printf(\n);}}9.打印以下图案:**********#includestdio.hvoidmain(){inti,j;printf(Thepictureis:\n);charpicture[4][4]={{'*'},{'*','*'},{'*','*','*'},{'*','*','*','*'}};for(i=0;i=3;i++){for(j=0;j=3;j++)printf(%c,picture[i][j]);printf(\n);}}10.求下列试子的值:1-1/2+1/3-1/4+……+1/99-1/100,将结果输出。#includestdio.hvoidmain(){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之间的所有水仙花数。#includestdio.hvoidmain(){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由键盘输入。#includestdio.hvoidmain(){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.打印以下图案:****************************#includestdio.hvoidmain(){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门课成绩;求出全班的总平均分,将结果输出。#includestdio.h#defineN4structstudent{charnum[3];charname[4];floatscore[2];floatavr;}stu[N];voidmain(){inti,j;floatsum,average;for(i=0;iN;i++)/*输入数据*/{printf(inputscoresofstudent%d:\n,i+1);printf(name:);scanf(%s,stu[i].name);for(j=0;j2;j++){printf(score%d:,j+1);scanf(%f,&stu[i].score[j]);}}average=0;/*计算*/for(i=0;iN;i++){sum=0;for(j=0;j2;j++)sum+=stu[i].score[j];stu[i].avr=sum/2;average+=stu[i].avr;}average/=N;printf(Namescore1score2average\n);for(i=0;iN;i++){printf(%5s%10s,stu[i].num,stu[i].name);for(j=0;j2;j++)printf(%9.2f,stu[i].score[j]);printf(%8.2f\n,stu[i].avr);}printf(average=%5.2f\n,average);}17.打印以下图案:*************************#includestdio.hvoidmain(){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.给出年、月、日,计算该日是该年的第几天。#includestdio.hvoidmain(){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;imonth;i++)day+=day_tab[i];return(day);}intleap(intyear){intleap;leap=year%4==0&&year%100!=0||year%400==0;return(leap);}19.求一个3*3的整型矩阵对角线元素之和。将原矩阵和求出的和输出。#includestdio.hvoidmain(){floata[3][3],sum=0;inti,j;printf(请输入元素:\n);for(i=0;i3;i++)for(j=0;j3;j++)scanf(%f,&a[i][j]);for(i=0;i3;i++)sum=sum+a[i][i];printf(对角线之和是:%6.2f\n,sum);for(i=0;i=3;i++){for(j=0;j=3;j++)printf(%5.2f,a[i][j]);printf(\n);}}20.求一个4*3的矩阵各行元素的平均值;将原矩阵和求出的平均值全部输出。#includestdio.hvoidmain(){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
本文标题:c语言实例111道
链接地址:https://www.777doc.com/doc-7028705 .html