二级c语言试题题库.docx
分享徐家祥的分享热门推荐hot(上篇下篇:徐家祥分享皿的日志113道C语言题目,超经典的今天16:40作者Zzt113道C语言题目,超经典的分享首次分享者:首次、ft弄晕已被分享8次评论(0)复制链接分享转载举报1 .输入两个正整数,m和n,求其最大公约数和最小公倍数。#include<stdio. h>void main()(int hcf(int, int) ;/*函数声明*/int lcd(int, int, int); /*函数声明*/int u, v, h, 1;printf (z,Please input two numbers:nz,);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,z, 1);)int hcf(int u,int v)int t, r;if(v>u)t=u;u=v;v=t;while(r=u%v)!=0)u=v;v=r;return(v);)int led(int u,int v,int h)(return(u*v/h);)2 .输入一行字符,分别统计出其中字母、空格、数字和其他字符的个数。#include<stdio. h>int letter, digit, space, others;void main()void count(chart);char text80;printf (z,Please input string:n,z);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:%dn,z, letter, digit, space, others);)void count(char str)(int i;for(i=0;stri !=,0,;i+)if (stri>=,a &&stri<=,z') | | (stri>=' A' &&stri<=,Z') letter+;else if (stri>=,&&stri<=,9')digit+;else if(stri=32)space+;elseothers+;3 .输入一个正整数求出它是儿位数;输出原数和位数。#include<stdio. h>int digit;void main()(void count(char);char text80;printf (z,Please input numbers:nz,);gets(text);printf("Numbers:n);puts (text);digit=O;count (text);printf (z/digit :%dnz,, digit);)void count (char strL)(int i;for (i=0;stri !=> 0* ;i+)if (str i>=,0) &&stri<=,9') digit+;)4,输入一个正整数,输出原数并逆序打印出各位数字。#include<stdio. h>void invertLonglnt(long);void main()(unsigned long iNumber;printf (z,Please input a number: n,z);scanf&iNumber);printf("The input number is:%ldn”, iNumber);printf(Z/The inverse number is:); invertLonglnt(iNumber);)void invertLonglnt(long x)(if(x>=0&&x<=9)printf (,%dn,z, x);elseprintfC%d,z,x%10);invertLonglnt(x/10);5 .从键盘上输入若干学生的一门课成绩,统计并输出最高成绩和最低成绩及相应 的序号,当输入负数时结束输入。6 .从键盘上输入若干学生的一门课成绩,计算出平均分,当输入负数时结束输入。 将结果输出。7 .求1!+2!+3!+20!,将结果输出。#include<stdio. h>void main()(float s=0,t=l;int n;for (n=l;n<=20;n+)(t=t*n;s=s+t;)printf("1!+2!+3!+20!=%en”, s);)8 .打印以下图案:*#include<stdio. h>void main()(int i, j;printf("The picture is:n");static char picture47=' ,,' ,,' ,,,*,,*,*,*,*,*,,,*,*,*,*,*,*,'*,;for(i=0;i<=3;i+)(for(j=0;j<=6;j+)printfpictureij);printf('n");9 .打印以下图案:*#include<stdio. h>void main()(int i, j;printf(""The picture is:n");char picture 4 4 = '*',for(i=0;i<=3;i+)(for(j=0;j<=3;j+)printf (/z%czz, picturei j);printf('n");10 .求下列试子的值:1T/2+1/3-1/4+1/99-1/100,将结果输出。#include<stdio. h>void main()(float sum=l. 0, t, s=l;int i;for(i=l;i<=100;i+)(t=s/i;sum=sum+t;s=-s;)printf ("1T/2+1/3T/4+l/99-l/100=%5.4fn”, sum);)11 .打印出100999之间的所有水仙花数。#include<stdio. h>void main()(int i, j, k, n;printf ("100999之间的所有水仙花数are: n*);for(n=100;n<1000;n+)i=n/100;j=n/10-i*10;k=n%10;if(n=i*i*i+j*j*j+k*k*k) printf (,z%d ”, n);)printf('n");)12 .求 Sn=a+aa+aaa+ +aa a 之值,n, a 由键盘输入。#include<stdio. h>void main()(int a, n, i=l, 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(/za+aa+aaa+ , ,+aa a=%dnzz, sn);)13 .打印以下图案:#include<stdio. h>void main()(char a7 = ,*,,*;*,*;*,*,/*,;int i, j, k;char space='printf("The picture is:");for (i=0;i<4;i+)(printf(n");printf (,z");for(j=l;j<=i;j+)printf (,z%c,z, space);for (k=O;k<7;k+)printf ak);14 .打印以下图案: 112112321 123432115 .打印以下图案:123432112321121 116 .编写一个统计学生成绩程序,完成以下功能:输入4个学生的2门课成绩; 求出全班的总平均分,将结果输出。#include<stdio. h>define N 4 struct student (char num3;char name4;float score2;float avr;)stuN;void main ()(int i, j;float sum, average;for(i=0;i<N;i+)/* 输入数据*/(printf C input scores of student%d:n”, i+1);printf("name:;scanfstui. name);for(j=0;j<2;j+)printf(*score %d:*, j+1);scanf&stui. scorej);/*计算*/average=O;for(i=0;i<N;i+)sum=0;for(j=0;j<2;j+)sum+=stui. scorej;stui. avr=sum/2;average+=stui. avr;)average/=N;printf C Name scorel score2 averagenz/);for(i=0;i<N;i+)(printf (z,%5s%10s,z, stui. num, stui. name);for(j=0;j<2;j+)printf C%9. 2f”, stui. scorej);printf ("%8. 2fn”, stui. avr);)printf (/zaverage=%5. 2fn,average);)17 .打印以下图案:#include<stdio. h>void main()(char a5 = ,int i, j, k;char space='printf("The picture is:);for(i=0;i<5;i+)(printf('n");printf();for(j=l;j<=i;j+)printf("%c,space);for(k=0;k<5;k+)printfak);18 .给出年、月、日,计算该日是该年的第几天。#include<stdio.h>void main()int sum_day(int, int);int leap(int year);int year, month, day, days;printf (z/ input date (year, month, day):z,);scanf (%d, %d, %d”, &year, &month, &day);printf ("%d/%d/%d”, year, month, day);days=sum_day(month, day);if(1eap(year)&&month>=3)days=days+1;printf (/zis the %dth day in this year. n,z, days);)int sum_day(int month, int day)(int day_tab13 = 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);int leap(int year)(int leap;leap=year%4=0&&year%100!=0|year%400=0;return(leap);)19 .求一个3*3的整型矩阵对角线元素之和。将原矩阵和求出的和输出。#include<stdio. h>void main()(float a33, sum=0;int i, j;printf (请输入元素:n);for(i=0;i<3;i+)for(j=0;j<3;j+)scanf &ai j);for(i=0;i<3;i+)sum=sum+aii;printf ("对角线之和是:%6. 2fn", sum); for(i=0;i<=3;i+)for(j=0;j<=3;j+)printf ("%5. 2f”, ai j); printf("n");20 .求一个4*3的矩阵各行元素的平均值;将原矩阵和求出的平均值全部输出。#include<stdio. h>void main()(int a4 3, s4, i, j, k;for(i=0;i<4;i+)for(j=0;j<3;j+)scanf*(a+i)+j);for(i=0;i<4;i+)k=0;for(j=0;j<3;j+)k+=ai j;printf ("第%d行的平均值是%d", i+1, k);printf('n");21 .求一个3*4的矩阵各列元素的平均值;将原矩阵和求出的平均值全部输出。#include<stdio. h>void main ()(int a43, s4, i, j, k;for(i=0;i<4;i+)for(j=0;j<3;j+)scanf*(a+i)+j);for(i=0;i<4;i+)(k=0;for(j=0;j<3;j+)k+=aij;printf ("第知行的平均值是%d", i+1, k);22 .求一个3*5的矩阵各列元素的最大值,将原矩阵和求出的最大值全部输出。#include<stdio. h>void main()(int a3 5, s3, i, j, k;for(i=0;i<3;i+)for(j=0;j<5;j+)scanf("%d,*(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 (z,Line=%d Max=%d,z, j, sj);printf("n);23.求一个4*3的矩阵各行元素的最大值,将原矩阵和求出的最大值全部输出。#include<stdio. h>void main()(int a43, s4,i,j, k;for(i=0;i<4;i+)for(j=0;j<3;j+)scanf("%d,*(a+i)+j);for(i=0;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 (zzRow=%d Max=%d*, i, si); printf('n");24 .求一个M*N的矩阵中元素的最大值,将原矩阵和求出的最大值全部输出。 #include<stdio. h> ttdefine N 10#define M 10 void main() ( int aj,k,max, m, n;scanf("%d %d”, &m, &n);for(i=0;i<m;i+)for(j=0;j<n;j+)scanf * (a+i)+j);for(i=0;i<m;i+)for(j=0;j<n-l;j+)(if(ai j<=aij+l)max=ai j+1;elsemax=aij;)ki=max;)for(i=0;i<m-l;i+)if(ki<=ki+U)max=ki+l;elsemax=ki;)printf (/,%dzz, max);)25 .判断一个N*N的矩阵是否为对称矩阵,将原矩阵输出,判断结果输出。 #include<stdio. h>Sdefine N 10void main()(int aN N, i, j, k, n; scanf&n);for(i=0;i<n;i+)for(j=0;j<n;j+) scanf&aij);for(i=0;i<n;i+)(for(j=i+l;j<n;j+)(if (ai j=aj i) k=l;elsek=0;if(k=O)printf("bushi");elseprintf("shi'n");for(i=0;i<n;i+)for(j=0;j<n;j+)printfaij);)26 .有一篇文章,有三行文字,每行有80个字符。要求统计出其中英文大写字母、 消协字母、数字、空格以及其他字符的个数。#include<stdio. h>void main()int i, j, big=O, sma=0, num=0, spa=0, oth=0;char text380;for(i=0;i<3;i+)printf (”请输入行%d:n”, i+1);gets (text i);for(j=0;j<80&&textij!=' 0'j+)(if (text i j>=,A' &&text i j<=,Z')big+;else if (text i j>=,a &&texti j<=,z )sma+;else if (text i j>=,O' &&texti j<=,9')num+;else if(textij=,') spa+;elseoth+;printf ("大写字母:%dn", big);printf ("小写字母:%dn”, sma);printf ("数字:%dn”, num);printf ("空格:刎n”, spa);printf ("其它:%dn”, oth);)27 .将20个整数放到一维数组中,输出该数组的最大值和最小值。#include<stdio. h>void main()(int i, j, min, max, a21;printf ("请输入数据:n");for(i=l;i<=20;i+)(printf(*a%d=*, i);scanf&ai);for(i=l;i<=19;i+)(min=i;for(j=2;j<=19;j+)if(amin>aj);amin=aj;)for(i=l;i<=19;i+)max=i;for(j=2;j<=19;j+)if(amax<ajl);amax=aj;printf ("最大数为:%dn*, a max);printf ("最小数为:%dnz,, a min);28 .将15个整数放到-维数组中,输出该数组中的最大值它的下标,然后将它和 数组中的最前面的元素对换。#include<stdio. h>void main()(int i, j, min, max, a16, m, n;printf ("请输入数据:n");for (i=l;i<=15;i+)(printf(“ai);scanf;)for(i=l;i<=14;i+)max=i;for(j=2;j<=14;j+)if(amax<aj);amax=aj;m=I, n=j)printf ("最大数下标为:%d, %dn”,m,n);29 .将字符数组strl种下标为偶数的元素赋给另一字符数组str2,并输出strl 和 str20#include<stdio. h>#include<string. h>Sdefine N 10void main()int i, n;char strlN,str2N;gets (strl);for (n=0;n<N;n+)(i=2*n;str2n=strli;)puts(strl);puts(str2);)30 .编写一个程序,将两个字符串连接起来,不要使用strcat函数。#include<stdio. h>void main()(char strl20, str220;int i=0,j=0;printf(请输入字符串l:rT);scanf(s”, strl);printf (请输入字符串2:n);scanfstr2);while (strli!='0')i+;while (str2j !=,0J)strli+=str2j+;strl i=> 09 ;printf (合并之后的字符串为:sn, strl);)31 .编写一个程序,将字符数组a中的全部字符复制到字符数组b中。不要使用 strcpy 函数。#include<stdio. h>#include<string. h>void main()(char a40, b40;int i;printf (请输入 a:rT);scanf a);for(i=0;i<=strlen(a);i+)bi=ai;printf (,zb:%snz/, b);)32 .编写一个程序,找出3个字符串中的最大者,将它输出。#include<stdio. h>#include<string. h>void main()char string20;char str320;int i;for (i=0;i<3;i+)gets (stri);if (strcmp(str0, strl)>0)strcpy (string, str0);elsestrcpy(string, strl);if(strcmp(str2, string) >0)strcpy(string, str2);printf (最大的字符串是:n%sn,string);33 .编写程序,输入任意一个17之间的整数,将他们转换成对应的英文单词。例如:1转换成Monday, 7转换成Sundayo#include<stdio.h>void main() int a;printf (输入一个整数:n");scanf (,z%dz,, &a);if(a=l) printf("Monday'n");else else else else else else elseif(a=2) if(a=3) if(a=4) if(a=5) if(a=6) if(a=7)printf("Tuesdayn);printf (/,Wendesdayn,z);printf ("Thursdayn);printf (z,Fridaynzz);printf("Saturday、。”); printf("Sundayn");printf (错误n);34 .编写程序,输入两个整数,和+、-、*、/之中的任意一个运算符,输出计算 结果。#include<stdio. h>void main()(float a, b;char c;printf(请输入一个运算符:n);scanf("%c", &c);printf ("请输入两个整数:n);scanf C%d, %d,z, &a, &b);switch(c)(case* +' :printf(fn”, a+b);break;case':printf (%fn”, a-b);break;case'*' :printf (fn”, a*b) ;break;case'/' :printf (,z%fn,z, a/b) ;break;default:printf (错误);35 .输入年号,计算这一年的2月份的天数,将结果输出。#include<stdio. h> void main()(int year, leap;printf (”请输入年份:n);scanf&year);if(year%4=0)(if(year%100=0)(if(year%400=0) leap=l;else leap=0;)else leap=l;)elseleap=0;if(leap)printf C%d 年的 2 月有 29 天,year);elseprintf C%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 (z,Please enter three sides of a triangle:n,z);scanf C%f, %f, %r, &a, &b, &c);s=(a+b+c)/2. 0;area=sqrt(s*(s-a)*(s-b)*(s-c);if(a+b<c|b+c<a|a+c<b)printf (Z/They can,t form a triangle. n,z); elseprintf (z,The area of the triangle is:%fnzz, area);#include<stdio. h>ttdefine N 10#define M 10 void main() (int i, j, k, m, n, f lagl, f lag2, aN,max, maxj;printf ("输入行数 nn<10:");scanf ("%d", &n);printf (输入列数 mm<10: );scanf("%d", &m);for(i=0;i<n;i+)(printf ("第d 行n”, i+1);for(j=0;j<m;j+)scanf&aij);37 .输入三角形的三边a, b,c,判断a,b,c,能否构成三角形,何种类型的三角 形:等腰、等边、直角、等腰直角、一般。#include<stdio. h>#include<math. h>void main()(float a, b, c;printf ("请输入三角形边长:n);scanf&a, &b, &c);if (a-b>=c) | | (b-c>=a) | | (c-a>=b) printf (不能够成三角形n);else if (a*a+b*b=二c*c) | | (b*b+c*c=二a*a)| | (c*c+a*a1=b*b) if (a=b) | | (b=c) | | (c=a) printf (等腰直角三角形n); else printf (直角三角形n);else if (a=b)&&(b=c) printf (等边三角形n);else if (a=b)&&(b!=c) | | (c=b)&&(b!=a) | | (a=c)&&(c! =a) printf ("等腰 三角形n);else printf ("一般三角形n");38 .输入奖金数a,计算并输出税率、应缴税款和实得奖金数。(用if语句编程)#include<stdio. h>void main()(int a;float r, t, s;printf ("请输入奖金数:n");scanf (绘d”, &a);if (a<500) r=0.00;else if (a<1000) r=0.05;else if (a<2000) r=0.08;else if (a<2000) r=0.10;else r=0. 15;t=a*r;s=a-t;printf ("税率:7. 2fn", r);printf ("税款:%7. 2fn t);printf ("实得奖金:7. 2fn", s);)39 .输入奖金数a,计算并输出税率、应缴税款和实得奖金数。(用switch语句 编程)#include<stdio. h>void mainO(int a, e;float r, t, s;printf ("请输入奖金数:n);scanf &a);e=a/500;switch(e)case 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;default:r=0. 15;break;)t=a*r;s=a-t;printf ("税率:%7.2f'n",r);printf ("税款:%7. 2fn", t);printf (实得奖金:%7. 2f'n", s);40 .输入学生的成绩,利用计算机将学生的成绩划分出等级并输出:90100: A 级;80-89: B 级;70-79: C 级;60-69: D 级;0 59: E 级;#include<stdio. h>void main()(float m;printf ("输入学生成绩:n");scanf &m);if (m>100) | | (m<0) printf ("数据有误n");else if (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.ttinclude <stdio. h>Sinclude <math. h>void main()(float a, b, c, t;double xl,x2;printf (请由高次到低次顺序输入系数:n);scanf C%f%f%r, &a, &b, &c);t=b*b4*a*c;if (t<0) printf ("方程无实根n");if (t=0)(xl=-(b/2/a);printf ("方程有两个相等实根,xl=x2=%5. 2fn", xl);;if (t>0)(xl=-(b+sqrt(t)/2/a;x2=-(b-sqrt(t)/2/a;printf ("方程有两个不等实根,xl=%5. 2f, x2=%5. 2fn", xl, x2);42 .编程序,输入4个整数,按由小到大的顺序输出。#include <stdio. h>#define N 4void main()int aN, t, i, j;printf ("输入四个整数:n");for (i=0;i<N;i+)scanf("%d”, &ai);printf("n");for (i=0;i<N-l;i+)(for (j=0;j<N-i-l;j+)if (aj>aj+U)t=aj;aj=aj+l;aj+l=t;printf ("排序后:n");for (i=0;i<N;i+) printf("%dn", ai);43 .求满足1+2+3+n<500中最大的N,并求其和,编写程序实现。#include <stdio. h>void main () (int n=0, sum=0:while(sum<500) (+n; sum+=n; )printf ('N?:%dn', n-l);printf C1+2+3+4+%d<500n”,n-1);44 .把100200之间的不能被3整除的数输出。ttinclude <stdio. h>void main()(int a;for (a=100;a<=20