您好,欢迎访问三七文档
1、输入两个数,输出较大数的平方值。#includestdio.hmain(){inta,b,t;printf(Pleaseentertwonumbers:);scanf(%d%d,&a,&b);if(ab){a=t;a=b;b=t;}printf(Theresultis:%d,b*b);getch();}2、计算并输出50-100间的数的和。#includestdio.hmain(){inti,sum;for(i=50;i=100;i++)sum=sum+i;printf(sumis%d,sum);getch();}3、用数组编程,有一个4×4的矩阵,编程序计算并输出对角线元素之和#includestdio.hmain(){inta[4][4],i,j,sum=0;printf(Pleaseenter16numbers:);for(i=0;i4;i++)for(j=0;j4;j++)scanf(%d,&a[i][j]);for(i=0;i4;i++)for(j=0;j4;j++)if(i==j)sum=sum+a[i][j];printf(Theresultis:%d,sum);getch();}4、编写程序,通过键盘输入2个整数,计算并输出它们的平均值。#includestdio.hmain(){inti,j,avg;printf(entertwonumber:/n);scanf(%d%d,&i,&j);avg=(i+j)/2;printf(avgis%d,avg);getch();}5、编写程序,在屏幕上输出字符串2011Ctest。#includestdio.h#includeconio.hmain(){printf(2011Ctest\n);getch();}6、铁路托运行李规定:行李重不超过50公斤的,托运费按每公斤0.15元计费;如超过50公斤,超过部分每公斤加收0.10元。编一程序完成输入行李重,计算并输出托运费。#includestdio.hmain(){inti,j;doublesum=0;printf(Pleaseentertheweight:);scanf(%d,&i);if(i=50)sum=0.15*i;else{j=i-50;sum=7.5+0.25*j;}printf(Theresultis:%.2f,sum);getch();}7、计算并输出50-100间的偶数和。#includestdio.h#includeconio.hmain(){inti,sum=0;for(i=50;i=100;i++)if(i%2==0)sum=sum+i;printf(sumis%d,sum);getch();}8、把10个整数装入一维数组,求其最大值及所在位置并输出。#includestdio.hmain(){inta[10],i,f;printf(Pleaseenter10numbers:);for(i=0;i10;i++)scanf(%d,&a[i]);f=0;for(i=0;i10;i++)if(a[i]a[f])f=i;printf(Theresultis:%d,locate:%d,a[f],f+1);getch();}9、输入三个大字母,将其转换成小写字母并输出。#includestdio.hmain(){chara[3];inti;printf(Pleaseenterthreeexpress:);gets(a);for(i=0;i3;i++)a[i]=a[i]+'a'-'A';printf(Theresultis:);puts(a);getch();}10、输入三个整数,如果能够构成三角形的三边,计算并输出三角形的周长,否则输出Dataerror!#includestdio.hmain(){inta,b,c,sum=0;doubled,f;printf(Pleaseenterthreeline:);scanf(%d%d%d,&a,&b,&c);d=(a+b+c)/2.0;f=(d-a)*(d-b)*(d-c);if(f0){sum=a+b+c;printf(s=%d,sum);}elseprintf(DATAERROR!);getch();}11、输入一行字符,统计并输出字母a出现的频数及频率。#includestdio.hmain(){chara[80];inti,j=0;printf(Pleaseenteraexpresses:);gets(a);for(i=0;i80;i++)if(a[i]=='a')j++;printf(Theresultis:%d,j);getch();}12、输入一个三位数,计算并输出各位数字平方之和。#includestdio.h#includeconio.hmain(){inti,sum=0,a,b,c;printf(enteronenumber:);scanf(%d,&i);a=i%10;b=(i/10)%10;c=i/100;sum=a*a+b*b+c*c;printf(sumis%d,sum);getch();}13、输入三个整数,输出大到小排序。#includestdio.hmain(){inta[3];inti,j,index,k;printf(Pleaseenterthreenumbers:);for(i=0;i3;i++)scanf(%d,&a[i]);for(i=0;i2;i++){index=i;for(j=i+1;j3;j++)if(a[j]a[index])index=j;k=a[index];a[index]=a[i];a[i]=k;}printf(Theresultis:);for(i=0;i3;i++)printf(%6d,a[i]);getch();}14、计算并输出[2,55]区间内奇数的平方和。#includestdio.hmain(){inti,sum=0;for(i=2;i=55;i++)if(i%2==1)sum=sum+i*i;printf(Theresultis:%d,sum);getch();}15、输入10个数据,按从小到大的顺序输出。#includestdio.hmain(){inta[10];inti,j,index,k;printf(Pleaseentertennumbers:);for(i=0;i10;i++)scanf(%d,&a[i]);for(i=0;i9;i++){index=i;for(j=i+1;j10;j++)if(a[j]a[index])index=j;k=a[index];a[index]=a[i];a[i]=k;}printf(Theresultis:);for(i=0;i10;i++)printf(%6d,a[i]);getch();}16、编写程序,输入华氏温度,计算并输出摄氏温度。(计算公式为:C=5/9(F-32))#includestdio.hmain(){intf,c;printf(Pleaseenterthef:);scanf(%d,&f);c=5*(f-32)/9;printf(Thecis:%d,c);getch();}17、设计一个简易计算器,根据输入运算符(+、*)的不同,计算两个数的和、积。#includestdio.hmain(){doublevalue1,value2;charoperator;printf(Typeinanexpression:);scanf(%lf%c%lf,&value1,&operator,&value2);switch(operator){case'+':printf(=%.2f\n,value1+value2);break;case'*':printf(=%.2f\n,value1*value2);break;}getch();}18、编程判断输入的一个字符串str是否为回文,所谓回文是指这个字符串正读和倒读都是一样的。如asdsa是回文。要求定义并调用函数fhuiwei(char*p)来完成判断的过程。#includestdio.h#includestring.hmain(){intfhuiwei(char*p);char*q;gets(q);if(fhuiwei(q)==1)printf(yes);elseprintf(no);getch();}intfhuiwei(char*p){intj;inti=0;char*q;q=p;while(*p!='\0'){p++;i++;}p--;for(j=0;j=i/2;j++){if(*p!=*q)break;q++;p--;}if(ji/2)return1;elsereturn0;}19、有10个整型元素的一维数组;求:(1)平均值。(2)统计低于平均值的元素个数并输出。#includestdio.hmain(){inta[10];inti,j=0,sum=0;doubleavg;printf(Pleaseentertennumbers:);for(i=0;i10;i++)scanf(%d,&a[i]);for(i=0;i10;i++)sum=sum+a[i];avg=sum/10;for(i=0;i10;i++)if(a[i]avg)j++;printf(Theaverageis:%.2f,Thenumberis:%d,avg,j);getch();}20编写程序,输入两个整数,计算并输出它们的立方和。#includestdio.h#includemath.hmain(){inta,b,sum=0;printf(entertwonumber:\n);scanf(%d%d,&a,&b);sum=pow(a,3)+pow(b,3);printf(sumis%d\n,sum);getch();}21、输入一元二方程的系数,计算并输出方程的实根。(保留2位小数)。#includestdio.hmain(){inta,b,c,d;doublex1,x2;printf(Pleaseenterthreenumbres:);scanf(%d%d%d,&a,&b,&c);d=b*b-4*a*c;if(d0){x1=((-b+sqrt(d))/2*a);x2=((-b-sqrt(d))/2*a);printf(x1=%.2f,x2=%.2f,x1,x2);}elseprintf(Noresult);getch();}22、计算并输出s=50+49+48+……+1。#includestdio.hmain(){inti,s=0;for(i=1;i=50;i++)s=s+i;printf(sis%d,s);getch();}23、用数组编程,有一个3×4的矩阵,编程序求出最大的那个元素的值,以及其所在的行号和列号。#includestdio.hmain(){inta[3][4];inti,j,h,k;printf(Pleaseenter12numbers:);for(i=0;i3;i++)for(j=0;j4;j++)scanf(%d,&a[i][j]);h=0;k=0;for(i=0;i3;i++)for(j=0;j4;j++)if(a[i][j]a[h][k]){h=i;k=j;}printf(Themaxis:%d,%d,%d,a[h][k],h,k);getch();}24、编写一个程序,其功能为:从键盘输入三个数x、y、z,判断x+y=z是否成立,若成立输出x+y=z的信息,否则输出x+y!=z的信息。#includestdio.hmain(){intx,y,z;printf(enterthreenumber:\n);scanf(%d%d%d,&x,&y,&z);if(x+y==z){printf(x+y==z);}else{pri
本文标题:c语言经典试题答案
链接地址:https://www.777doc.com/doc-5069193 .html