经典c语言例题.docx
1 .输入两个正整数,m和n,求其最大公约数和最小公倍数。#include<stdio.h> void main()(int hcf(inljnt);/*函数声明int lcd(intjnt.int);/*函数声明*/int u,v,h J;printf("Please input two numbers:nH);scanf("%d,%dn,&u,&v);h=hcf(u,v);printf("H.C.F=%dn'h);l=lcd(u,v,h);printf("L.C.D=%dnMJ);int hcf(int uJnt v)iniif(v>u)(t=u;u=v;v=t;while(r=u%v)!=O)u=v;v=r;)return(v);1int lcd(int u,int vjnt h)(return(u*v/h);)2 .输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#include<stdio.h>int letteredigit,space,others;void main()void count(char);char text8O;printf("Please input string:nM);gets(text);printfCtringAn");puts(text);letter=O;digit=O;space=O;others=0;count(text);printf("letter:%d.digit:%d,space:%d,others:%dn r'Jetter,digit,space,others);)void count(char str)int i;for(i=0;stri!='0'i+)if(stri>='a,&&stri<=,z,)ll(stri>='A'&&stri<=,Z')letter+;else if(stri>='0'&&stri<=,9,)digit+;else if(stri=32)space+;elseothers+;(3 .输入一个正整数求出它是几位数;输出原数和位数。#include<stdio.h>int digit;void main()(void count(char);char text(8O;printf("Please input numbers:nH);gets(text);printf("Numbers :n");puts(text);digit=O;count(text);printf(Mdigit:%dnM,digit);)void count(char str|)(int i;for(i=0;stri!=0,;i+)if(stri>='0,&&stri<=,9,)digit+;)4 .输入个正整数,输出原数并逆序打印出各位数字。#include<stdio.h>void invertLongInt( long);void main() unsigned long iNumber; printff'Please input a numbcr:n");scanf("%ki”,&iNumber);printf("The input number is:%ldn",iNumber);printf("The inverse number is:");invertLonglnt(iNumber);void in vert Longlnt( long x)if(x>=0&&x<=9)printf("%dn",x);else(printf("%d",x%10);invertLong!nt(x/10);)(5 .从键盘上输入若干学生的门课成绩,统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入。6 .从键盘上输入若干学生的一门课成绩,计算出平均分,当输入负数时结束输入.将结果输出。7 .求1!+2!+3!+20.将结果输出。#include<stdio.h>void main()(float s=O,t=l;int n;for(n=1;n<=20;n+)(t=t*n;s=s+t;)printf(" l!+2!+3!+20!=%en",s);)8 .打印以下图案:*#include<stdio.h>void main()(int ij;printf("Thc picture is:n");static char picture47=r V V*1), for(i=0;i<=3;i+) for(j=0;j<=6;j+)printf("%cH,picturei|j);printf("nH);)9 .打印以卜图案:*#include<stdio.h>void main()(int ij;printf("The picture is:n");char picture44= I'*1),for(i=0;i<=3;i+)(for(j=0;j<=3;j+)printf(M%cM,pictureij);printf("nw);)10 .求下列试子的值:1-1/2+1/3-1/4+1/99-1/100,将结果输出。#include<stdio.h>void main()(float sum=1.0,t,s=l;int i;for(i=l;i<=100;i+)(t=s/i;sum=sum+t;s=-s;)printf("l -1/2+1/3-1/4+ l/99-l/100=%5.4An,sum);)IL打印出100999之间的所有水仙花数。#include<stdio.h>void main()(ini ij,k,n;printf("100-999之间的所有水仙花数are:n");for(n=100;n<10(X);n-H-) 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 由键盘输入。#include<stdio.h>void main()(int a,n,i=l,sn=O,tn=O;printf("a,n=:M);scanf("d,%d”,&a,&n);while(i<=n)tn=tn+a;sn=sn+(n;a=a*10;+i;)printf("a+aa4-aaa+.+aa.a=%dn'sn);113 .打印以下图案:*#include<stdio.h>void main()int ij,k;char space=*printf("The picture is:");for(i=0;i<4;i+)printf("nH);primf("");for(j=l;j<=i;j+)printf("%cH,space);for(k=0;k<7;k+) printf("%c'ak);)printfC'Xn1');)14 .打印以卜图案:112112321123432115 .打印以下图案:123432112321121116 .编写一个统计学生成绩程序,完成以下功能:输入4个学生的2门课成绩;求出全班的总平均分,将结果输出。#include<stdio.h>#define N 4struct studentIchar num3;char name4;float score2;float avr;1stu|N|;void main()(int ij;float sum,average;for(i=0:i<N;i+)/*输入数据*/(printf(" input scores of student%d:n,i+l);printf("namc:');scanf("%s*',stu i.name);for(j=0;j<2;j+)printfC,score %d:",j+l);scanf(H%r,&stui.score|j );) average=O;/*计算*/ for(i=0;i<N;i+)sum=O;for(j=0;j<2;j+)sum+=stui,score|j;stui.avr=sum/2;a verage+=st u i . av r;1average/=N;printf(" Name score 1 score2 averagen");for(i=0;i<N;i+)(printf("%5s%10s,stui.num,stui.name);for(j=0;j<2;j+)printf("%9.2fstui.scorej);prlntfC'%&2fn%tui.avr);Ip rin tf(" a vera ge=%5.2fn 'a verage);17 .打印以卜图案:*#include<stdio.h>void main()chara5=咒中产'/产'int i,j,k;char space:'printf("The picture is:");fbr(i=O;i<5;i+)(printf("ntr);printf(" H);for(j=l;j<=i;j+)printf("%c",space);for(k=0;k<5;k+) printf("%c",ak);)printf("n,r);18 .给出年、月、日,计算该日是该年的第几天。#include<stdio.h>void main()int sum_day(intjnt);int leap(int year);int year,month,day ,days;printfC* input date(year,month,day):*');scanf('*%d,%d,%dn,&year,&month,&day);printf("%d/%d/%d",year,month,day);day s=sum_day (month .day);if(leap(year)&&month>=3)days=days+l;printf("is the %dth day in this year.n",days);Iint sum_day(int monthjnt day)(int day_tabl 3=0,31,28,31,30,31,30,31,31,30,31,30,31);int i;for(i=l ;i<month;i+)day+=day_tabi;return (day);Iint leap(int year)(int leap;leap=year%4=0&&year%100!=0llyear%400=0;return(leap);119 .求个3*3的整型矩阵对角线元素之和。将原矩阵和求出的和输出。#include<stdio.h>void main()(float a33,sum=0;int ij;printff请输入元素:n");for(i=0;i<3;i+)for(j=0;j<3;j+)scanfC%f;&aij);for(i=0;i<3;i+)sum=sum+a|i|i;printf("对角线之和是:%6.2AnM,sum);for(i=0;i<=3;i+)(for(j=0;j<=3;j+)printf("%5.2f;aij);printf("nH);20 .求一个4*3的矩阵各行元素的平均值:将原矩阵和求出的平均值全部输出。#include<stdio.h> void main()(inta43,s4,i,j,k;for(i=0;iv4;i+) for(j=0;j<3;j+) scanf(”%d”,*(a+i)+j);for(i=0;i<4;i+)(k=0;for(j=0;j<3;j+)k+=aiU;printf("第d行的平均值是d”,i+l,k);printf("nH);II21 .求一个3*4的矩阵各列元素的平均值:将原矩阵和求出的平均值全部输出。#include<stdio.h>void main()(inta43,s4,i,j,k;for(i=();i<4;i+) for(j=0;j<3;j+) scanf("%d”,*(a+i)+j);for(i=0;i<4;i+)(k=0;for(j=0;j<3;j+)k+=aiU;printf("第d行的平均值是d”,i+l,k);printfC'Xn1');22 .求个3*5的矩阵各列元素的最大值,将原矩阵和求出的最大值全部输出。#include<stdio.h> void main() ini a35,s3,i,j,k;for(i=0;i<3;i+)for(j=0;j<5;j+)scanf(1,%d,1,*(a+i)+j);for(i=0;i<3;i+)*(s+i)=*(*(a+j);for(j=l;j<5;j+)if(*(s+i)<*(*(a+i)+j)*(s+i)=*(*(a+i)+j);)for(i=0;i<3;i+)(printf("Line=%d Max=%d'j,sj);printf("nH);)23 .求一个4*3的矩阵各行元素的最大值,将原矩阵和求出的最大值全部输出。#include<stdio.h>void main()inta43Ls4j,j,k;for(i=0;i<4;i+)for(j=0;j<3;j+) scanfC%d",*(a+i)+j);for(i=();i<4;i+)*(s+i)=*(*(a+i);for(j=l;j<3;j+) if(*(s+i)<*(*(a+i)+j)*(s+i)=*(*(a+i)+j);)for(i=0;i<4;i+)(printf("Row=%d Max=%d",i,si);printf(Hn,r);(24 .求个M*N的矩阵中元素的最大值,将原矩阵和求出的最大值全部输出。#include<stdio.h>#define N 10#define M 10void main()int a,max,m,n;scanf(M%d %d",&m,&n);fbr(i=O;i<mu+)for(j=0;j<n;j+)scanf("%d”,*(a+i)4j);for(i=0;i<m;i+)(for(j=0;j<n-l;j+)(if(ai(j<=aij+l)max=ai|j+l ;elsemax=ai|j;(ki=max;for(i=0;i<m-1;i+)(if(k|il<=ki+l|)max=ki+l ;elsemax=ki;1printf("%d",max);25 .判断一个N*N的矩阵是否为对称矩阵,将原矩阵输出,判断结果输出。#include<stdio.h>#define N 10void main()(inta|NNUj,k,n;scanf(H%d'&n);for(i=0;i<n;i+)for(j=0;j<n;j+)scanf(H%dH,&aij);for(i=0;i<n;i+)(for(j=i+l;j<n;j+)(if(aiU=aUi)k=l;elsek=0;if(k=O) printf("bushiM); elseprintf("shi'n");for(i=0;i<n;i+) for(j=0;j<n;j+) printf(M%dM,aij);)26 .有一篇文章,有三行文字,每行有80个字符。要求统计出其中英文大写字母、消协字母、数字、空格以及其他字符的个数。#include<stdio.h> void main()int i,j,big=0,sma=0,num=0,spa=0,oth=0;char text380: for(i=0;i<3;i+)( printf("请输入行d:n",i+l); gets(texl|i);for(j=0;j<80&&texti|j!=,0,;j+)(if(textij>='A,&&textij<='Z,) big+;else if(textij>='a'&&texti|j<='z,) sma+;else if(textij>=,0,&&textij<='9,) num+;else if(textij'*)spa+; elseoth+;) printfC 大写字母:%dn”,big); printf("小写字母:dn".sma); printf("数字:%dn",num); printf("空格:%dn",spa); printf("其它:dn loth);)27 .将20个整数放到一维数组中,输出该数组的最大值和最小值。#include<stdio.h> void main()int i,j,min,max,a21;printfC请输入数据An");for(i=l;i<=20;i+)printf("a%d=,i);scanf(H%d,&ai);)for(i=l;i<=19;i-H-)(min=i;for(j=2;j<=19;j+)if(amin>a|j);amin=a|j;)fdr(i=l;i<=19;i+)(max=i;for(j=2;j<=19;j-H-)if(a(max<aj);amax=a|j;)printf("最大数为:%dn'amax);printf("最小数为:%dnamin);128 .将15个整数放到维数组中,输出该数组中的最大值它的下标,然后将它和数组中的最前面的元素对换。#include<stdio.h>void main()(int i,j,min,max,a16,m,n;printf("请输入数据An");for(i=l;i<=(printf("a%d=i);scanf(n%d",&ai);)fbr(i=l;i<=14;i-H-)(max=i;for(j=2;j<=14;j+) if(amax<aj);amax=aj; m=I,n=jprintf("最大数下标为:%d,%dn'm,n);)29 .将字符数组strl种下标为偶数的元素赋给另一字符数组str2,并输出strl和市2。#include<stdio.h>#include<string.h>#defineN 10void main()int i,n;char strlN,str2N;gets(strl);for(n=0;n<N;n-H-)(i=2*n;str2n=strl i;puts(strl);puts(str2);I30 .编写个程序,将两个字符串连接起来,不要使用stirat函数。#include<stdio.h>void main()(char strl20,str220;ini i=0,j=();printf("请输入字符串l:n");scanf(n%sM,strl);printf("请输入字符串2:n");scanf("%s",str2);while (strli!=W)i+;while (str2j!=W)strli+=str2U+;strlti>W;printf(H合并之后的字符串为:%sn ”,str 1);)31 .编写一个程序,将字符数组a中的全部字符复制到字符数组b中。不要使用stirpy函数。#include<stdio.h>#include<string.h>void main()(char a40,b40;int i;printfC请输入 a:nM);scanf(H%s",a);for(i=0;i<=strlen(a);i+)bi=ai;printf("b:%sn,b);)32 .编写一个程序,找出3个字符串中的最大者,将它输出。#include<stdio.h>#include<string.h>void main()(char string2O;charstr32O;int i;for(i=0;i<3;i+)gets (stri);if(strcmp(strO,strl )>0)strcpy(string,strO);elsestrcpy(string,str|11);if(strcmp(str2,string)>0)strcpy(string,str|2);printf("最大的字符串是:n%sn”,string);)33 .编写程序,输入任意一个17之间的整数,将他们转换成对应的英文单词。例如:1转换成Monday转换成Sunday。#include<stdio.h>void main()int a;primf("输入一个整数:nM);scanf("%d&a);if(a=l) printfCMondayVn1');else if(a=2) prinlf(',Tuesdayn,');else if(a=3) printf(uWendesdaynM);else if(a=4) printf(HThursdayn");else if(a=5) printf(,FridaynH);else if(a=6) printfC,SaturdayXn*');else if(a=7) printf(MSundaynH);else printf("错误n");)34 .编写程序,输入两个整数,和+、-、*、/之中的任意一个运算符,输出计算结果。#include<stdio.h>void main() float a,b;char c;printf(“请输入一个运算符:n”);scanf(H%c",&c);printf("请输入两个整数:n");scanf(H%d,%d'&a,&b);switch(c)(case'+':printf(,'%fn",a+b);break;case'-':printf("%fn",a-b);break;case,*,:printf("%fn,a*b);break;case'/':printf(H%fn",a/b);break;default:printf(M®误");135 .输入年号,计算这一年的2月份的天数,将结果输出。#inchide<stdio.h>void main()ini yearjeap;printf(”请输入年份:n");scanf(H%d",&year);if(year%4=0)(if(year%100=0)(if(year%400=0)leap=1;elseleap=0;elseleap=1;)elseleap=0;if(leap)printf("%d 年的2月有29天”,year);elseprintf("%d 年的2月有28天”,year);)36 .输入三角形的三边a.b,c,判断a.b,c,能否构成三角形,若能,计算面积。#include<stdio.h>#include<math.h>void main()float a,b,c,area;double s;printf("Please enter three sides of a triangle:n'*);scanf(H%f,%f,%f,&a,&b,&c);s=(a+b+c )/2.0;area=sqrt(s*(s-a)*(s-b)*(s-c);if(a+b<cllb+c<alla+c<b)printf("They can't form a triangleAn11); elseprintf("The area of the triangle is:%fn",area); I#include<stdio.h>#define N 10#define M 10 void main()(int ij,k,m,n,flag 1,flag2,aN,max,maxj;printf("输入行数 nn<10:");scanf(H%d'&n);printfC输入列数 mm<10:H);scanf("%d'&m);for(i=0;i<n;i+)(printf("第d 行 XiTj+l);for(j=0;j<m;j-H-)scanf("%dH,&aij);)37 .输入三角形的三边a,b,c,判断a,b,c,能否构成三角形,何种类型的三角形:等腰、等边、直角、等腰直角、一般。#include<stdio.h>#include<math.h> void main() float a,b,c;printf("请输入三角形边长An");scanf(H%f%f%f,&a,&b,&c);if(a-b>=c)ll(b-c>=a)ll(c-a>=b) printf("不能够成三角形n");else if (a*a+b*b=c*c)ll(b*b+c*c=a*a)ll(c*c+a*a=b*b) if (a=b)ll(b=c)ll(c=a) printf("等腰直角三角形n");else printf("直角三角形n“);else if(a=b)&&(b=c) prinlf("等边三角形n");else if(a=b)&&(b!=c)ll(c=b)&&(b!=a)ll(a=c)&&(c!=a) printf("等腰三角形n”);else printf("般三角形n");38 .输入奖金数a,计算并输出税率、应缴税款和实得奖金数。(用if语句编程)#include<stdio.h> void main()(int a;float r,t,s;printf("请输入奖金数An”); scanf("%d”,&a);if (a<500) r=0.00;else if (a<1000) r=0.05;else if(a<20()0)=0.08;else if(a<20()0)=0.10;else r=0.15;t=a*r;s=a-t;printff'税率:7.2fn",r);printff'税款:printfC 实得奖金:7.2fn”,s);)39 .输入奖金数a,计算并输出税率、应缴税款和实得奖金数。(用switch语句编程)#include<stdio.h>void main()(int a,e;float r,t,s;printfC请输入奖金数:n");scanf("%d",&a);e=a/500;switch(e)case 0:r=0.00;break;case 1 :n=0.05;break;case 2case 3:r=O.O8;break;case 4:case 5:case 6:case 7:case 8:case 9:r=0.10;break:default:r=0.15;break: t=a*r;s=a-t;printf("税率:%7.2fn",r);printf("税款:%7.2fn",t);printf("实得奖金:%7.2fn",s);)40 .输入学生的成绩,利用计算机将学生的成绩划分出等级并输出:90100: A 级;8089: B 级;70-79: C 级;6069: D 级;059: E 级;#include<stdio.h>void main()(float m;primf("输入学生成绩:n");scanf("%f',&m);if(m>100)ll(m<0) printf("数据有误n");else if (m>=90) printf(*'A 级n");else if (m>=80) printf(HB 级n");else if (m>=70) printf(级n");else if (m>=60) printf(nD 级n");else printf(ME 级n");I41 .编程序,求方程aX2+bX+c=0的解:输入a,b,c.#include <stdio.h>#include <math.h>void main()(float a,b,c,t;double xI,x2;printf(”请由高次到低次顺序输入系数:n");scanf(H%f%f%f,&a.&b,&c);t=b*b-4*a*c;if(t<0) printf("方程无实根n");if (t=0)(xl=-(b/2/a);printf("方程有两个相等实根,xl=x2=%5.2AnM,xl);;if (t>0)(xl=-(b+sqrt(t)/2/a;x2=-(b-sqrt(t)/2/a;printf("方程有两个不等实根,xl=%5.2f,x2=%5.2fnxl,x2);42 .编程序,输入4个整数,按由小到大的顺序输出。#include <stdio.h>#dcfine N4 void main() intprintf("输入四个整数An"); for (i=0;i<N;i-H-)scanf(H%d",&ai);printf("nn);for (i=0;i<N-l;i+)(for (j=O;j<N-i-l if(aj>aj+n)(aU=aU+l;aj+l=t;1( printf("排序后:VT);for (i=0;i<N;i-H-) printf("%dn",ai);143 .求满足l+2+3+.+n<500中最大的N,并求其和,编写程序实现。#include <stdio.h> void main()(int n=O,sum=O;while(sum<500)+n;sum+=n;)printf("N?:%dn'n-l);printf("1+2+344+%d<500n,n-l);)44 .把100200之间的不能被3整除的数输出.#include <stdio.h> void main()( int a;for (a=100;a<=200;a+)if (a%3!=0) printf("%dt",a);45 .求Fibonacci数列前40个数,每行输出5个,将40个Fibonacci数输出。#include <stdio.h> long f(int); void main() int n,i=0;fbr (n=l;n<41;n+)printf("ldt”,f(n); i+;if(i%5=0) printf(,n,'); I 1 long f(int m) if (m=0) return 0;if (m=l) return 1; elsereturn f(m-l )+f(m-2);)46 .按以下规律翻译密码:将每一个字母变成它后面的字母,例如,将A变成B, B变成C,,Z变成A,非字母字符不变,”作为电文结束标志。#include <stdio.h> void main()( char ch;printf("输入字符串:n”); while (ch=getcharO)!='!")(if (ch>='a'&& ch<='z')ll(ch>='A,&& ch<=T) if (ch='z') ch='a'else if(ch=,Z,) ch='A' else ch=ch+l;printf('*%c",ch);) printf("nH);)47 .百元买百鸡问题:公鸡每只5元,母鸡每只3元,小鸡3只一元,问一百元买一百只鸡有几种买法。#include <stdio.h> void main() ini a,b,c,n=0;for (a=0;a<=2();a+)for