计算机程序设计基础(C语言)编程习题
更新时间:2023-11-22 23:34:01 阅读量: 教育文库 文档下载
计算机程序设计基础(c语言) 习题 编程题
计算机程序设计基础(C语言)
编程练习题及参考答案
1.输入2个整数,求两数的平方和并输出。
#include
{ intt a ,b,s;
printf(\ scanf(\ s=a*a+b*b;
printf(\ is %d\\n\ }
2. 输入一个圆半径(r)当r>=0时,计算并输出圆的面积和周长,否则,输出提示信息。
#include
#define PI 3.14
main()
{ float r ,s , l;
printf(\ scanf(\
if (r>=0) {s=pi*r*r; l=2*i*r ;
printf(\
printf(\else
printf(\ }
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(\if (x<0) y=1;
else if(x==0) y=5; else y=3;
printf(\
6.从键盘输入两个数,求出其最大值(要求使用函数完成求最大值,并在主函数中调用该函数)
main()
{float max(float x,float y); float a,b,m;
scanf(\
m=max(a,b);
printf(\}
float max(float x,float y) {
float temp; if (x y=temp; } return(x); } 2 计算机程序设计基础(c语言) 习题 编程题 7、从键盘输入你和你朋友的年龄,编成判断谁的年龄最大,并打印最大者的年龄。 #include { int yourAge, hisAge; printf(\ scanf(\ /*输入你的年龄yourAge*/ printf(\ scanf(\ /*输入你朋友的年龄hisAge*/ if (yourAge >= hisAge) { printf(\ } if (hisAge > yourAge) { printf(\ }} 8、键盘输入2个加数,再输入答案,如果正确,显示“right”,否则显示“error” #include “stdio.h” main( ) {int a,b,c; printf(“please input a and b\\n”); scanf (%d,%d”,&a,&b); printf(“please input the answer for a+b\\n”); scanf (%d”,&c); if (c==a+b) printf(“right\\n”); else printf(“error\\n”); } 9. 编一程序每个月根据每个月上网时间计算上网费用,计算方法如下: 30元?? 费用??每小时3元?每小时2.5元?main() { int hour; float fee; printf(“please input hour:\\n”); 3 ?10小时10?50小时 ?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 (“神州行话费为: %f\\n”,x); printf (“全球通话费为: %f\\n”,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 income\\n”); 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 int data; char grade; printf(\ scanf(\ 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(\s %c”,grade); } *13. 编程设计一个简单的计算器程序。从键盘输入2个操作数,1个运算符,当运算符为加(+)、减(-)、乘(*)、除(/)时,输出计算结果 5 计算机程序设计基础(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 *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(\ printf(\n”); } } *27.编写两个函数分别求两个整数的最小公倍数和最大公约数,用主函数调用这两个函数并输出结果。两个整数由键盘输入。 #include \ 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(\ch=getchar(); printf(\ scanf(\if(ch=='1') c=mingb(a,b); else if(ch='2') c=maxgy(a,b); printf(\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(\ for(i=0;i<10;i++) scanf(\ printf(\for(i=1;i<10;i++) for(j=0;j<9;j++) if(a[j] for(i=0;i<10;i++) printf(\} 30. 定义一个5行3列的数组,从键盘输入各数组元素的值,计算各数组元素之和。 #include main( ) { int i, j ,a[5][3]; printf(“Enter data:\\n”); for(i=0;i<5;i++) for(j=0;j<3;j++) scanf(“%d”,&a[i][j]); 13 计算机程序设计基础(c语言) 习题 编程题 for(i=0;i<5;i++) for(j=0;j<3;j++) sum=sum+a[i][j]; printf(“sum=]\\n”,sum); } 31、编写程序,交换两个数组中的对应元素。 #include { int a[N], b[N], i, j, temp; printf(“please input a:\\n”); for(i=0; i scanf(“%d”, &a[i]); printf(“please input b:\\n”); for(j=0; j scanf(“%d”, &b[i]); for(i=0; i for(j=0; j for(j=0; j *32、从键盘上输入一个4*3的整型数组,找出数组中的最小值及其在数组中的下标。 #include { int a[4][3], i , j ,min,m,n; printf(\ for (i=0; i<4; i++) for (j=0; j<3; j++) scanf(“%d”,& a[i][j]); min=a[0][0]; m=0; n=0; for (i=0; i<4; i++) for (j=0; j<3; j++) if (a[i][j] 14 计算机程序设计基础(c语言) 习题 编程题 {min= a[i][j]; m=i; n=j; } printf(\ printf(\ %d \\n, m,n); } 33.编程实现如下功能:从键盘输入一行字符,统计其中大写英文字符,小写英文字符和其他字符的个数。 #include #define ARR_SIZE 80 main() { char str[ARR_SIZE]; int len, i, letter = 0, digit = 0, space = 0, others = 0; printf(\ string:\ gets(str); len = strlen(str); for (i=0; i { if (str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z') letter ++; /*统计英文字符*/ else if (str[i] >= '0' && str[i] <= '9' ) digit ++; /*统计数字字符*/ else others ++; /*统计其它字符的个数*/ } printf(\ %d\\n\ printf(\ %d\\n\ printf(\ %d\\n\} *34.编程实现如下功能: 1)在主函数中,实现从键盘输入10名学生某门课的成绩,保存在一维数组中;调用排序函数;对排序后的数组中的元素按从高到低打印输出。 2)编写排序函数,使用数组名做函数参数,实现对该成绩的排序。 #include void Sort(float score[], long num[], int n); 15
正在阅读:
计算机程序设计基础(C语言)编程习题11-22
《容忍与自由》读后感09-10
医院院长任期经济责任审计述职报告材料04-22
我的辅导班作文600字06-24
生理学习题04-22
江苏省农村电子商务发展调研报告05-23
在籍证明03-31
电子工艺实习报告 - 图文09-16
在2022年县委县政府联合督查推进工作会议上的讲话范文04-03
少儿国画教学计划11-02
- exercise2
- 铅锌矿详查地质设计 - 图文
- 厨余垃圾、餐厨垃圾堆肥系统设计方案
- 陈明珠开题报告
- 化工原理精选例题
- 政府形象宣传册营销案例
- 小学一至三年级语文阅读专项练习题
- 2014.民诉 期末考试 复习题
- 巅峰智业 - 做好顶层设计对建设城市的重要意义
- (三起)冀教版三年级英语上册Unit4 Lesson24练习题及答案
- 2017年实心轮胎现状及发展趋势分析(目录)
- 基于GIS的农用地定级技术研究定稿
- 2017-2022年中国医疗保健市场调查与市场前景预测报告(目录) - 图文
- 作业
- OFDM技术仿真(MATLAB代码) - 图文
- Android工程师笔试题及答案
- 生命密码联合密码
- 空间地上权若干法律问题探究
- 江苏学业水平测试《机械基础》模拟试题
- 选课走班实施方案
- 习题
- 程序设计
- 语言
- 编程
- 计算机
- 基础
- 清华大学j精密仪器与机械学航空宇航科学与技术考研 招生人数 参考书 报录比 复试分数线 招生简章 考研大纲
- C语言程序设计期末考试卷A答案
- 初级会计实务三色笔记
- 2017年武汉市八年级下学期期末数学重点题型训练
- 201804《新能源汽车概论》专一期中试卷
- 第二章 生命活动调节测试卷(一)整理版含答案
- 2015年工作总结-压缩版 - 图文
- 补充作业答案
- 南京工程学院图书馆
- 2016年包装机械制造行业现状及发展趋势分析
- 具有时滞的传染病动力学模型
- 《附中的研究性学习评价方案》必选案例分析
- 二(8)后进生工作总结2
- 广东职师计算机系信息管理电子商务01级专科班学籍表
- 物流公司经营目标责任书
- 工期延期情况说明
- 能知而不知法者不免责
- 成本会计复习资料及解析
- XX学校学生营养餐工作总结交流材料
- 《传播学教程》教案