admin 管理员组文章数量: 1086019
2024年4月22日发(作者:html怎么写弹窗)
计算机程序设计基础(c语言) 习题 编程题
计算机程序设计基础(C语言)
编程练习题及参考答案
1.输入2个整数,求两数的平方和并输出。
#include
main()
{ intt a ,b,s;
printf("please input a,b:n");
scanf("%d%d”,&a,&b);
s=a*a+b*b;
printf("the result is %dn",s);
}
2. 输入一个圆半径(r)当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。
#include
#define PI 3.14
main()
{ float r ,s , l;
printf("please input r:n");
scanf("%f”,&r);
if (r>=0)
{s=pi*r*r;
l=2*i*r ;
printf("the area is %fn",s);
printf("the circumference is %fn",l);}
else
printf("input error!n");
}
3、函数y=f(x)可表示为:
2x+1 (x<0)
y= 0 (x=0)
2x-1 (x>0)
编程实现输入一个x值,输出y值。
main()
{int x,y;
scanf(“%d”,&x);
If(x<0)y=2*x+1;
If(x>0)y=2*x-1;
If(x==0) y=0;
printf(“%d”,y);}
1
计算机程序设计基础(c语言) 习题 编程题
4、编写一个程序,从4个整数中找出最小的数,并显示此数。
main( )
{int a,b,c,d,t;
scanf (“%d,%d,%d,%d ”,&a,&b,&c,&d);
if (a>b)
{t=a; a=b; b=t;}
if (a>c)
{t=a; a=c; c=t;}
if (a>d)
{t=a; a=d; d=t;}
printf (“min = %d n”,a);
}
5.有一函数当x<0时y=1,当x>0时,y=3,当x=0时y=5,编程,从键盘输入一个x值,
输出y值。
main()
{int x,y;
scanf("%d",&x);
if (x<0) y=1;
else if(x==0) y=5;
else y=3;
printf("x=%d,y=%dn",x,y);}
6.从键盘输入两个数,求出其最大值(要求使用函数完成求最大值,并在主函数中调用该函数)
main()
{float max(float x,float y);
float a,b,m;
scanf("%f,%f",&a,&b);
m=max(a,b);
printf("Max is %fn",m);
}
float max(float x,float y)
{
float temp;
if (x {temp=x; x=y; y=temp; } return(x); } 2 计算机程序设计基础(c语言) 习题 编程题 7、从键盘输入你和你朋友的年龄,编成判断谁的年龄最大,并打印最大者的年龄。 #include main() { int yourAge, hisAge; printf("Please enter your age:"); scanf("%d", &yourAge); /*输入你的年龄yourAge*/ printf("Please enter your friend's age:"); scanf("%d", &hisAge); /*输入你朋友的年龄hisAge*/ if (yourAge >= hisAge) { printf("You are older! Your age is = %dn", yourAge); } if (hisAge > yourAge) { printf("Your friend is older! HisAge age is = %dn", hisAge); }} 8、键盘输入2个加数,再输入答案,如果正确,显示“right”,否则显示“error” #include “stdio.h” main( ) {int a,b,c; printf(“please input a and bn”); scanf (%d,%d”,&a,&b); printf(“please input the answer for a+bn”); scanf (%d”,&c); if (c==a+b) printf(“rightn”); else printf(“errorn”); } 9. 编一程序每个月根据每个月上网时间计算上网费用,计算方法如下: 30元 费用 每小时3元 每小时2.5元 main() { int hour; float fee; printf(“please input hour:n”); 3 10小时 1050小时 50小时 要求当输入每月上网小时数,显示该月总的上网费用(6分) 计算机程序设计基础(c语言) 习题 编程题 scanf(“%d”,&hour); if(hour<=10) fee=30; else if(hour>=10&&hour<=50) fee=3*hour; else fee=hour*2.5; printf(“The total fee is %f”,fee); } 10.神州行用户无月租费,话费每分钟0.6元,全球通用户月租费50元,话费每分钟0. 4元。 输入一个月的通话时间,分别计算出两种方式的费用,判断哪一种合适。 main() {float a,x,y; printf(“n请输入您的话费:”); scanf(“%f,”,&a); x= 0.6*a; y=50+0.4*a; printf (“神州行话费为: %fn”,x); printf (“全球通话费为: %fn”,y); if (x>=y) printf(“建议使用全球通”); else printf(“建议使用神州行); } 11.个人所得税计算,应纳税款的计算公式如下: 收入 收入<=1000元部分 2000元>=收入>1000元的部分 3000元>=收入>2000元的部分 6000元>=收入>3000元的部分 收入>6000元的部分 税率 0% 5% 10% 15% 20% 输入某人的收入,计算出应纳税额及实际得到的报酬。(7分) (如需连续计算多个人的纳税情况,直到输入负数为止,程序应如何改进?试写出程序) #include “stdio.h” main() { int grade; float income,tax,money; printf(“please input your incomen”); scanf (“%f”,&income); 4 计算机程序设计基础(c语言) 习题 编程题 if (income<0) printf(“the input is error”); else { grade=(int)income/1000; switch(grade) { case 0 : tax=0;break; case 1 : tax=(income-1000)*0.05;break; case 2 : tax=50+(income-2000)*0.1;break; case 3 : case 4 : case 5 : tax=150+(income-3000)*0.15;break; default: tax=600+(income-6000)*0.2; } money=income-tax; printf(“n tax=%f, money=%f”,tax, money); } } 12.从键盘上输入一个百分制成绩score,按下列原则输出其等级:score≥90,等级为A; 80≤score<90,等级为B;70≤score<80,等级为C;60≤score<70,等级为D;score<60, 等级为E。 #include main() { int data; char grade; printf("Please enter the score:"); scanf("%d”, &data); switch(data/10) { case 10: case 9 : grade=’A’; break; case 8: grade=’B’; break; case 7: grade=’C’; break; case 6: grade=’D’; break; default: grade=’E’; } printf("the grade is %c”,grade); } *13. 编程设计一个简单的计算器程序。从键盘输入2个操作数,1个运算符,当运算符为加(+)、 减(-)、乘(*)、除(/)时,输出计算结果 5 计算机程序设计基础(c语言) 习题 编程题 #include main() { int data1, data2; /*定义两个操作符*/ char op; /*定义运算符*/ printf("Please enter the expression:"); scanf("%d%c%d", &data1, &op, &data2); /*输入运算表达式*/ switch(op) /*根据输入的运算符确定要执行的运算*/ { case '+': /*处理加法*/ printf("%d + %d = %d n", data1, data2, data1 + data2); break; case '-': /*处理减法*/ printf("%d - %d = %d n", data1, data2, data1 - data2); break; case '*': /*处理乘法*/ printf("%d * %d = %d n", data1, data2, data1 * data2); break; case '/': /*处理除法*/ if (0 == data2) /*为避免出现溢出错误,检验除数是否为0*/ printf("Division by zero!n"); else printf("%d / %d = %d n", data1, data2, data1 / data2); break; default: printf("Unknown operator! n"); } } 14. 从键盘输入10个整数,统计其中正数、负数和零的个数,并在屏幕上输出。 main( ) {int a[10], i,p=0,n=0,z=0; printf(“please input number”); for(i=0;i<10;i++) {scanf(“%d,”,&a[i]); if (a[i]>0) p++; else if (a[i]<0) n++; else z++} printf(“正数:%5d, 负数:%5d,零:%5dn”,p,n,z); } } 6 计算机程序设计基础(c语言) 习题 编程题 15、编程序实现求1-200之间的所有数的乘积并输出。 #include main( ) { int i, sum=1 for(i=1; i<200 i=i+1) sum=sum*i; printf(“the sum of odd is :%d”,sum); } 16. 从键盘上输入10个数,求其平均值。 main() { int a[10],i,s=0; float ave;; for(i=0;i<10;i++) scanf(“%d”,&a[i]); for(i=0;i<10;i++) sum+=a[i]; ave=(float)sum/10; printf("ave = %fn", ave); } 17、编程序实现求1-1000之间的所有奇数的和并输出。 #include main( ) { int i, sum=0; for(i=1; i<1000; i=i+2) sum=sum+i; printf(“the sum of odd is :%d”,sum); } 18.有一个分数序列:2/1,3/2,5/3,8/5,13/8,21/13…… 编程求这个序列的前20项之和。 main() { int i,t,n=20; float a=2,b=1,s=0; for(i=1;i<=n;i++) {s=s+a/b; t=a; a=a+b; 7 计算机程序设计基础(c语言) 习题 编程题 b=t; } printf("sum=%9.6f",s); } 19. 用数组实现以下功能:输入5个学生成绩,而后求出这些成绩的平均值并显示出来。 main() {float a[5],i; float s=0; for(i=0;i<5;i++) scanf(“%f”,&a[i]); for(i=0;i<5;I++) s=s+a[i]; printf(“result=%f”,s/5); } *20、用循环的方法构造一个5行5列的二维数组,使主对角线上的变量为1,其它为0,并将 数组中所有项按行按列显示出来。 main() {int a[5][5],i,j, s=0; for(i=0;I<5;i++) for(j=0;j<5;j++) if(i= =j) a[i][j]=1; else a[i][j]=0; for(i=0;i<5;i++) for(j=0;j<5;j++) {if(j= =0) printf(“n”); printf(“%d ”, a[i][j]); } } 21.求一个3×3矩阵对角线元素之和。从键盘输入矩阵元素的值并输出和的值. main() { int a[3][3],sum=0; int i,j; printf("Enter data:n"); for(i=0;i<3;i++) for(j=0;j<3;j++) scanf("%d",&a[i][j]); for(i=0;i<3;i++) sum=sum+a[i][i]; printf("sum=%d",sum); } 8 计算机程序设计基础(c语言) 习题 编程题 22.输入n的值,n代表行数,输出如图所示的图形。(6分) * * * * * * * * * * * * * * * * (此图为n=4时的输出结果) #include main() {int i , j , k; for (i = 1; i <= 4; i++) /*控制行数*/ { for (k = 1; k <= (2 * i - 1); k++) /*控制每行输出的*号个数*/ { printf("*"); } printf("n"); }} /*输出一行后换行*/ 23、从键盘输入30名学生的成绩数据,求其中的最高分、最低分和平均分。 (提示:用数组存放成绩数据) #include #define M 30 main ( ) { float score[M], max , min, aver; int i ; printf(“please input score: n”); for(i=0; i scanf(“%f”, &score[i]); max=score[0]; min=score[0]; aver=score[0]; for(i=1; i { if (max < score[i]) max= score[i]; if (min>score[i]) min=score[i]; aver+=score[i]; } printf(“max=%f, min=%f,aver=%f”, max, min, aver/M); } 24. 从键盘输入某班学生某门课的成绩及其学号(班级人数最多40人,具体人数由键盘输入), 输出该班最高分和最低分及其学生学号;并输出该班该课程的总分和平均分。请编写程序。 #include #define ARR_SIZE 40 main() { float score[ARR_SIZE], maxScore,minScore,sum; 9 计算机程序设计基础(c语言) 习题 编程题 int n, i; long maxNum, minNum,num[ARR_SIZE]; printf("Please enter total number:"); scanf("%d", &n); printf("Please enter the number and score:n"); for (i=0; i scanf("%ld%f", &num[i], &score[i]); maxScore = score[0];minScore= score[0]; maxNum = num[0]; minNum= num[0]; sum=score[0]; for (i=1; i { if (score[i] > maxScore) { maxScore = score[i]; maxNum = num[i]; } { minScore = score[i]; minNum = num[i]; } sum=sum+score[i]; } printf("maxScore = %.0f, maxNum = %ldn", maxScore, maxNum); printf("sum = %.1f, average = %.1fn", sum, sum/n); } *25.将一个有5个元素的数组中的值(整数)按逆序重新存放。 例: 原来顺序为:8、6、5、4、1,要求改为1、4、5、6、8 define N 5 main() {int a[N],I,temp; printf(“enter array a:n”); for(I=0;I scanf(“%d”,$a[i]); for(I=0;I 10 else if (score[i] < minScore) printf("minScore = %.0f, minNum = %ldn", minScore, minNum); 计算机程序设计基础(c语言) 习题 编程题 { temp=a[i]; a[i]=a[N-I-1]; a[N-I-1]=temp; } printf(“n Now, array a:n”); for(I=0;I printf(“%4d”,a[i]); printf(“n”); } *26.从键盘上输入一个2*3的矩阵,将其转置后形成3*2的矩阵输出。 main() {int a[2][3], b[3][2],i,j; for(i=0;i<2;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); for(i=0;i<3;i++) for(j=0;j<2;j++) b[i][j]=a[j][i]; for(i=0;i<3;i++) {for(j=0;j<2;j++) printf("%5d",b[i][j]); printf("n”); } } *27.编写两个函数分别求两个整数的最小公倍数和最大公约数,用主函数调用这两个函数并输 出结果。两个整数由键盘输入。 #include "stdio.h" mingb(x,y) int x,y; {int z,i,t; z=1; i=1; if(x>y) {t=x;x=y;y=t;} while(z<=x*y) { z=i*y; if((z%x==0)&&(z%y==0)) break; i++; 11 计算机程序设计基础(c语言) 习题 编程题 } return(z); } maxgy(x,y) int x,y; {int z,t; if(x>y) {t=x;x=y;y=t;} z=x; while(z>1) { if((x%z==0)&&(y%z==0)) break; z--; } return(z); } main() { int a,b,c; char ch; printf("nmingb(1)/maxgy(2)?"); ch=getchar(); printf("ninput:"); scanf("%d,%d",&a,&b); if(ch=='1') c=mingb(a,b); else if(ch='2') c=maxgy(a,b); printf("the result is %d",c); getch(); } *28. 输入一个3*3矩阵,求出其转置矩阵,并求出两个矩阵的和. main() { int a[3][3]; int b[3][3]; int c[3][3] int i,j; printf(“please input 6 numbers!”) for (i=1;i<3;i++) for(j=1;j<3;j++) { scanf(“%d”,&a[i][j]); 12 计算机程序设计基础(c语言) 习题 编程题 b[j][i]=a[i][j]; } for (i=1;i<3;i++) for(j=1;j<3;j++) { c[i][j]=a[i][j]+b[i][j]; } for (i=1;i<3;i++) for(j=1;j<3;j++) { printf(“%d”,a[i][j]); } } 29、从键盘输入10名学生的成绩数据,按成绩从高到低的顺序排列并输出。(提示:用数组存 放成绩数据) main() { int a[10]; int i,j,temp; printf("input score:n"); for(i=0;i<10;i++) scanf("%d",&a[i]); printf("n"); for(i=1;i<10;i++) for(j=0;j<9;j++)
版权声明:本文标题:计算机程序设计基础(C语言)编程习题 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://roclinux.cn/p/1713785236a651413.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论