《100道c语言题.docx》由会员分享,可在线阅读,更多相关《100道c语言题.docx(121页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。
1、1 .输入两个正整数,m和n,求其最大公约数和最小公倍数。#includevoid ma in ()(int hcf (int, int);/*函数声明/int led (int, int, int);/函数声明/int u, v, h,1;pr intf (Please input two numbers:n);scanf (%d,%d,&u,&v);h=hcf (u, v);printf (H. C. F=%dn,h);l=lcd (u, v, h);printf (L. C. D=%dn,1);int hcf (int u, int v)(int t, r;if (vu)t=u; u=v
2、; v=t;whi le (r=u%v)!=0)u=v; v=r; return (v);int led (int u, int v, int h)(return (u*v/h);2 .输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#includeint letter,digit,space, others;void ma in ()(void count (char );char text 80;pr intf (Please input string:n);gets (text);pr intf (string:n);puts (text);letter=0;digi t=
3、0;space=0;others=0; count (text);printf (letter:%d, digit:%d, space:%d, others:%dn, letter, dig it, space, others);void count (char str )(int i;for (i=0; str !=,0z; i+)if (str=,az &str= A&str= U )letter+;else if (str=O&str=9)digi t+;else if(str=32)space+;elseothers+;3 .输入一个正整数求出它是几位数;输出原数和位数。#includ
4、eint digit;void main() void count (char );char text 80;pr intf (Please input numbers:n);gets (text);pr intf(Numbers:n);puts (text);digi t=0;count (text);printf (digit:%dn, digit);void count (char str )(int i;for (i=0; str!=; i+)if (str= O&str=9)digit+;4 .输入个正整数,输出原数并逆序打印出各位数字。#include void invertLon
5、glnt(long);void ma in () unsigned long iNumber;pr intf (Please input a number:n);scanf(%1d,&iNumber);pr intf (The input number is:%ldn, iNumber);pr intf (The inverse number is:);invertLonglnt (iNumber);void invertLonglnt(long x)(if (x=0&x=9)pr intf (%dn, x);else(pr intf (%d, x%10);invertLonglnt(x/10
6、);5 .从键盘上输入若干学生的门课成绩,统计并输出最高成绩和最低成绩及相应的序号,当输入负数时结束输入。6 .从键盘上输入若干学生的门课成绩,计算出平均分,当输入负数时结束输入。将结果输出。7 .求1!+2!+3!+20!,将结果输出。#includevoid ma in ()(float s=0, t=l;int n;for (n=l; n=20; n+)(t=t*n;s=s+t;)printf (l!+2!+3!+20!=%en, s);8 .打印以下图案:*#includevoid ma in ()(int i, j;pr intf (The picture is:n);static
7、char picture 47=z ,1*,for (i=0; i=3; i+)(for (j=0; j=6; j+)pr intf (%c, picture j);pr intf(n);9 .打印以下图案:12 *3 *4 *#includevoid ma in () int i, j;pr intf (The picture i s:n);char picture 44=*,,*/,,*/,/*/,/*/,/*,, I*,/*/,/*/,/*,;for (i=0; i=3; i+)(for (j=0; j=3; j+)pr intf (%c, picture j);printf(n);10
8、 .求下列试子的值:1T/2+11/4+1/99-1/100,将结果输出。#includevoid ma in ()(float sum=l.0, t, s=l;int i;for (i=l; i=100; i+)t=s/i;sum=sum+t;s=-s;printf (1-1/2+1/3-1/4+l/99-l/100=%5.4fn, sum);11 .打印出10999之间的所有水仙花数。#includevoid ma in ()(int i, j, k, n;printf (10999之间的所有水仙花数are:n);for (n=100; n1000; n+)(i=n/100;j=n/10-
9、i*10;k=n%10;if (n=i*i*i+j*j*j+k*k*k)printf (%d , n);pr intf (n);12 .求 Sn=a+aa+aaa+.+aa.a 之值,n, a 由键盘输入。#includevoid ma in ()(int a, n, i=l, sn=0, tn=0;pr intf (a, n=:);scanf (%d,%d,&a,&n);while (i=n)(tn=tn+a;sn=sn+tn;a=a*10;+i;pr intf (a+aa+aaa+aa a=%dn, sn);13 .打印以下图案:#includevoid ma in ()(char a7=
10、int i, j,k;char space=/;pr intf (The picture i s:);for (i=0; i4; i+)(printf(n);pr intf ();for (j=l; j=i; j+)pr intf(%c, space);for (k=0; k7; k+)printf(%c,a k);)pr intf (n);14 .打印以下图案:112112321123432115 .打印以下图案:123432112321121116 .编写个统计学生成绩程序,完成以下功能:输入4个学生的2门课成绩;求出全班的总平均分,将结果输出。#include#def ine N 4 s
11、truct student (char num 3;char name4;float score 2;float avr;stu N;void ma in ()(int i, j;float sum, average;for (i=0; iN; i+)/输入数据/(pr intf ( input scores of student%d:n, i+1);pr intf (name:);scanf (%s, stu. name);for (j=0; j2; j+)(printf (score %d:,j+1);scanf (%f,&stu. score j);)average=0;/计算/for
12、(i=0; iN; i+)sum=0;for (j=0; j2; j+)sum+=stu. score j;stu. avr=sum/2;average+=stu. avr;)average/=N;averagen);printf ( Name scorel score2for (i=0; iN; i+)(pr intf (%5s%10s, stu. num, stu. name);for (j=0; j2; j+)pr intf (%9.2f,s tu. score j);printf (%8.2fn, stu. avr);)printf (average=%5.2fn, average);
13、)17 .打印以下图案:#includevoid ma in ()(char a 5=;int i, j, k;char space=z ;pr intf (The picture i s:);for (i=0; i5; i+)(printf(n);pr intf ();for (j=l; j=i; j+)pr intf(%c, space);for (k=0; k5; k+)printf(%c,a k);pr intf (n);18 .给出年、月、日,计算该日是该年的第几天。#includevoid ma in () int sum-day (int, int);int leap(int y
14、ear);int year, month, day, days;pr intf ( input date (year, month, day):);scanf (%d,%d,%d,&year,&month,&day);pr intf (%d/%d/%d, year, month, day);days=sum_day(month, day);if (leap (year)&month=3)days=days+l;printf(is the %dth day in this year.n,days);)int sum-day (int month, int day)(int day-tab13=0
15、,31,28,31,30,31,30,31,31,30,31,30,31;int i;for (i=l; imonth; i+)day+=day_tab;return (day);int leap(int year)int leap;leap=year%4=0&year%100!=0I|year%400=0;return (leap);)19 .求一个3*3的整型矩阵对角线元素之和。将原矩阵和求出的和输出。#includevoid ma in ()(float a 33, sum=0;int i, j;printf (请输入元素:n);for (i=0; i3; i+)for (j=0; j3
16、; j+)scanf (%f,&a j);for (i=0; i3; i+)sum=sum+a;printf (对角线之和是:%6.2fn, sum);for (j=0; j=3; j+)printf (%5.2f,aj);pr intf (n);想拥有一个特别的主页么?紫豆音乐播放器,让你的音乐响起来,让你的主页动起来!胡华2楼胡华2008-04-1014:17回复 编辑 发站内信 删除20 .求一个4*3的矩阵各行元素的平均值;将原矩阵和求出的平均值全部输出。#includevoid ma in ()(int a 43, s 4, i, j, k;for (i=0; i4; i+)for
17、(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的矩阵各列元素的平均值;将原矩阵和求出的平均值全部输出。#includevoid ma in ()(int a 43, 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;p
18、r intf (第d行的平均值是%d, i+1, k);22,求一个3*5的矩阵各列元素的最大值,将原矩阵和求出的最大值全部输出。#includevoid ma in ()(int a 35, s 3, i, j, k;for (i=0; i3; i+)for (j=0; j5; j+)scanf (%d,*(a+i)+j);for (i=0; i3; i+)*(s+i)=*(*(a+j);for (j=l; j5; j+)if (*(s+i)*(*(a+i)+j)*(s+i)=*(*(a+i)+j);for (i=0; i3; i+)(printf (Line=%d Max=%d, j, s
19、j);printf (n);23.求一个4*3的矩阵各行元素的最大值,将原矩阵和求出的最大值全部输出。#includevoid ma in ()(int a 43, 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+)*(s+i)=*(*(a+i);for (j=l; j3; j+)if (*(s+i)*(*(a+i)+j)*(s+i)=*(*(a+i)+j);)for (i=0; i4; i+)(pr intf (Row=%d Max=%d, i, s );pr intf (n
20、);24.求一个M*N的矩阵中元素的最大值,将原矩阵和求出的最大值全部输出。#include#def ine N 10#def ine M 10 void ma in ()int a MN, i, j, k M, max, m, n;scanf (%d %d,&m,&n);for (i=0; im; i+)for (j=0; jn; j+) scanf (%d,*(a+i)+j);for (i=0; im; i+)(for (j=0; jn-l; j+)(if (a j=a j+1)max=a j+1;elsemax=a j;k=max;for (i=0; im-l; i+)(if (k=ki
21、+l)max=k i+1;elsemax=k;25.判断个N*N的矩阵是否为对称矩阵,将原矩阵输出,判断结果输出。#include#def ine N 10void ma in ()(int a NN, i, j, k, n;scanf (%d,&n);for (i=0; in; i+)for (j=0; jn; j+)scanf(%d,&a j);for (i=0; in; i+)(f or (j=i+l; jn; j+)(if (a j=a j)k=l;elsek=0;if (k=0)pr intf(bushi);elseprintf (shin);for (i=0; in; i+)for
22、 (j=0; jvoid ma in ()(int i, j, big=0, sma=0, num=0, spa=0, oth=0;char text 380;for (i=0; i3; i+)printf (请输入行d:n, i+1);gets (text);for (j=0; j= A&text j= a&text j= O&text j=9) num+;else if (textj=)spa+;else oth+;)printf (大写字母:%dn, big);printf (小写字母:%dn, sma);printf (数字:%dn, num);printf (空格:%dn, spa)
23、;printf (其它:%dn, oth);2.将20个整数放到维数组中,输出该数组的最大值和最小值。#include void ma in ()(int i, j, min, max, a 21;pr intf (请输入数据:n);for (i=l; i=20; i+)(pr intf (a %d=, i);scanf (%d,&a);for (i=l; i=19; i+)(min=i;for (j=2; ja j);a min=a j;)for (i=l; i=19; i+)(max=i;for (j=2; j=19; j+)if (a maxa j);a max=a j;pr intf
24、(最大数为:%dn, a max);printf (最小数为:%dn, a min);28 .将15个整数放到维数组中,输出该数组中的最大值它的下标,然后将它和数组中的最前面的元素对换。#includevoid ma in ()(int i, j, min, max, a 16, m, n;printf (请输入数据:n);for (i=l; i=15; i+)(printf (a %d=, i);scanf (%d,&a);for (i=l; i=14; i+)max=i;for (j=2; j=14; j+)if (a maxa j);a max=a j;m=I, n=j)printf (
25、最大数下标为:%d,%dn, m, n);)29 .将字符数组strl种下标为偶数的元素赋给另一字符数组str2,并输出strl和str2o#include#include#def ine N 10void main()(int i, n;char strl N,str2N;gets (strl);for (n=0; nN; n+)(i=2*n;str2n=strl;puts (strl);puts (str2);樊铁立3楼樊铁立2008-04-1014:17回复 编辑 发站内信 删除谢了胡华4楼胡华2008-04-1014:17回复 编辑 发站内信 删除30 .编写一个程序,将两个字符串连接
26、起来,不要使用strcat函数。#includevoid ma in ()(char strl 20, str220;int i=0, j=0;printf (请输入字符串l:n);scanf (%s,strl);printf (请输入字符串2:n);scanf str2);while (strl!=、)i+;while (str2j!=0)strli+=str2j+;strl=0;printf (合并之后的字符串为:%sn, strl);31 .编写个程序,将字符数组a中的全部字符复制到字符数组b中。不要使用strcpy函数。#include#include void main() char
27、 a 40, b 40;int i;pr intf (请输入 a:n);scanf (%s, a);for (i=0; i=str len (a); i+)b=a;pr intf (b:%sn, b);32 .编写一个程序,找出3个字符串中的最大者,将它输出。#include#includestring. hvoid main()(char string 20;char str 320;int i;for (i=0; i0)strcpy (string, str 0);elsestrcpy (string, str 1);if (strcmp (str 2, string)0)strcpy (
28、string, str 2);printf (最大的字符串是:n%sn, string);33 .编写程序,输入任意个1之间的整数,将他们转换成对应的英文单词。例如:1转换成Monday,7转换成Sunday。#include void ma in () int a;printf (输入个整数:n);scanf (%d,&a);if(a=l) printf (Mondayn);else if (a=2) printf (Tuesdayn);else if (a=3) printf (Wendesdayn);else if (a=4) printf (Thursdayn);else if (a=
29、5) pr intf (Fr idayn);else if (a=6) printf (Saturdayn);else if(a=7) printf (Sundayn);else pr intf (错误、n);34 .编写程序,输入两个整数,和+、/之中的任意个运算符,输出计算结果。#includevoid ma in ()(float a, b;char c;printf (请输入一个运算符:n);scanf (%c,&c);printf (请输入两个整数:n);scanf (%d,%d,&a,&b);switch (c)(case+: printf (%fn, a+b); break;ca
30、se printf (%fn, a-b); break;case*: printf (%fn, a*b); break;case/: printf (%fn, a/b); break;default: printf (错误;35 .输入年号,计算这一年的2月份的天数,将结果输出。#includevoid ma in ()(int year, leap;printf (请输入年份:n);scanf (%d,&year);if (year%4=0)(if(year%100=0)(if (year%400=0)leap=l;elseleap=0;)elseleap=l;leap=O;if (leap
31、)pr intf (%d 年的2月有29天,year);elsepr intf (%d 年的2月有28天,year);36 .输入三角形的三边a, b,c,判断a,b,c,能否构成三角形,若能,计算面积。#include#includevoid ma in ()(float a, b, c, area;double s;printf (Please enter three sides of a triangle:n);scanf (%f,%f,%f,&a,&b,&c);s=(a+b+c)/2.0;area=sqrt (s*(s-a)*(s-b)*(s-c);if (a+bcI Ib+caI|a
32、+cb)pr intf (They canz t form a triangle.n);pr intf (The area of the triangle is:%fn,area);#include#def ine N 10#def ine M 10void ma in ()(inti, j, k, m, n, f lagl, f lag2, a NM, max, max j;pr intf (输入行数 nn10:);scanf (%d,&n);pr intf (输入列数 mm10:);scanf (%d,&m);for (i=0; in; i+)(printf (第%d 行、n, i+1);
33、for (j=0; jm; j+)scanf (%d,&a j);37 .输入三角形的三边a, b,c,判断a,b,c,能否构成三角形,何种类型的三角形:等腰、等边、直角、等腰直角、一般。#include#includemath. hvoid ma in ()(float a, b, c;printf (请输入三角形边长:n;scanf (%f%f%f,&a,&b,&c);if (a-b=c) I I (b-c=a) I I (c-a=b) printf (不能够成三角形、n);else if (a*a+b*b=c*c)I I(b*b+c*c=a*a)I I(c*c+a*a=b*b)if (a
34、=b) I I (b=c) I I (c=a) printf (等腰直角三角形n);else printf (直角三角形n);else if (a=b)&(b=c) printf (等边三角形、n);eIseif (a=b)&(b!=c) I I (c=b)&(b!=a) I I (a=c)&(c!=a)printf (等腰三角形、n);else printf (一般三角形、n);38 .输入奖金数a,计算并输出税率、应缴税款和实得奖金数。(用if语句编程)#includestdio. h void main()int a;float r, t, s;pr intf (请输入奖金数:n);sc
35、anf (%d,&a);if (a500) r=0.00;elseif(a1000)r=0.05;elseif(a2000)r=0.08;elseif(a2000)r=0.10;else r=0.15;t=a*r;s=a-t;printf (税率:7.2fn, r);pr intf (税款:%7.2fn,t);printf (实得奖金:7.2fn,s);39 .输入奖金数a,计算并输出税率、应缴税款和实得奖金数。(用switch语句编程)#includeint a, e;float r, t, s;pr intf (请输入奖金数:n);scanf &a);e=a/500;switch (e)c
36、ase 0: r=0.00; break;case 1: r=0.05; break;case 2:case 3: r=0.08; break;case 4:case 5:case 6:case 7:case 8:case 9: r=0.10; break;defaul t: 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 级;889: B 级;70-79: C 级;60-69:
37、D 级;0-59: E 级;#includevoid ma in ()(float m;printf (输入学生成绩:n);scanf (%f,&m);if (m100) I I (m=90) printf (A 级、n);else if (m=80) printf (B 级、n);else if (m=70) printf (C 级n);else if (m=60) printf (D 级、n);else printf (E 级n);41 .编程序,求方程aX2+bX+c=0的解;输入a,b,c.#include #include void main()(float a, b, c, t;d
38、ouble xl, x2;printf (请由高次到低次顺序输入系数:、n;scanf (%f%f%f,&a,&b,&c);t=b*b-4*a*c;if (t0)xl=-(b+sqrt(t)/2/a;x2=-(b-sqrt (t)/2/a;printf (方程有两个不等实根,xl=%5.2f, x2=%5.2fn, xl, x2);42 .编程序,输入4个整数,按由小到大的顺序输出。#include #def ine N 4void ma in ()(int a N, t, i, j;printf (输入四个整数:n);for (i=0; iN; i+)scanf (%d,&a);pr intf (n);for (i=0; iN-l; i+)(for (j=0; ja j+1)(t=a j;a j=a j+1;a j+l=t;)pr intf (排序后:n);for (i=0;