资源描述
本答案仅供参考,请勿用于商业用途或进行未经同意的转载
第3周
1
计算两个数的平方和(3分)
题目内容:
从键盘读入两个实数,编程计算并输出它们的平方和,要求使用数学函数pow(x,y)计算平方值,输出结果保留2位小数。
提示:使用数学函数需要在程序中加入编译预处理命令 #include <math.h>
以下为程序的运行结果示例:
please input x and y:
1.2,3.4↙
result=13.00
输入格式: "%f,%f"
输出格式:
输入提示信息:"Please input x and y:\n"
输出格式:"Result=%.2f\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h> <math.h>
int main()
{
float x,y,S,D;
printf("Please input x and y:\n");
scanf("%f,%f",&x,&y);
S = pow(x,2);
D = pow(y,2);
printf("Result=%.2f\n",S+D);
return 0;
}
2
逆序数的拆分计算(3分)
题目内容:
从键盘输入一个4位数的整数,编程计算并输出它的逆序数(忽略整数前的正负号)。例如,输入-1234,忽略负号,由1234分离出其千位1、百位2、十位3、个位4,然后计算4*1000+3*100+2*10+1 = 4321,并输出4321。再将得到的逆序数4321拆分为两个2位数的正整数43和21,计算并输出拆分后的两个数的平方和的结果。
以下是程序的运行结果示例:
Input x:
-1234↙
y=4321
a=43,b=21
result=2290
输入格式: "%d"
输出格式:
输入提示信息:"Input x:\n"
逆序数输出格式:"y=%d\n"
逆序数拆分后的输出格式:"a=%d,b=%d\n"
平方和的输出格式:"result=%d\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h> <math.h>
main()
{
int x,b0,b1,b2,b3,y,a,b,c;
printf("Input x:\n");
scanf("%d",&x);
x = (int)fabs(x); /*取x绝对值*/
b3 = x/1000; /*取x千位*/
b2 = (x-b3*1000)/100; /*取x百位*/
b1 = (x-b3*1000-b2*100)/10; /*取x十位*/
b0 = x%10; /*取x个位*/
y = b3+b2*10+b1*100+b0*1000;
printf("y=%d\n",y);
a = b0*10+b1;
b = b2*10+b3;
c = (a*a)+(b*b);
printf("a=%d,b=%d\n",a,b);
printf("result=%d\n",c);
return 0;
}
3
拆分英文名(3分)
题目内容:
从键盘输入某同学的英文名(小写输入,假设学生的英文名只包含3个字母。如: tom),编写程序在屏幕上输出该同学的英文名,且首字母大写(如: Tom)。同时输出组成该英文名的所有英文字符在26个英文字母中的序号。
以下为程序的运行结果示例:
Input your English name:
tom↙
Tom
t:20
o:15
m:13
输入格式: "%c%c%c"
输出格式:
输入提示信息:"Input your English name:\n"
首字母大写的英文姓名的输出格式:"%c%c%c\n"
姓名中每个字母在26个英文字母中的序号的输出格式:"%c:%d\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h> <math.h>
int main()
{
char x,y,z,A;
printf("Input your English name:\n");
scanf("%c%c%c\n",&x,&y,&z);
A=x-32; /*首字母改为大写*/
printf("%c%c%c\n",A,y,z);
printf("%c:%d\n",x,x-96);
printf("%c:%d\n",y,y-96);
printf("%c:%d\n",z,z-96);
}
4
计算体指数(3分)
题目内容:
从键盘输入某人的身高(以厘米为单位,如174cm)和体重(以公斤为单位,如70公斤),将身高(以米为单位,如1.74m)和体重(以斤为单位,如140斤)输出在屏幕上,并按照以下公式计算并输出体指数,要求结果保留到小数点后2位。
假设体重为w公斤,身高为h米,则体指数的计算公式为:
以下是程序的运行结果示例:
Input weight, height:
70,174↙
weight=140
height=1.74
t=23.12
输入格式: "%d,%d"
输出格式:
输入提示信息:"Input weight, height:\n" (注意:在height和逗号之间有一个空格)
体重输出格式:"weight=%d\n"
身高输出格式:"height=%.2f\n"
体指数输出格式:"t=%.2f\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h> <math.h>
int main()
{
float A,b;
int x,y;
printf("Input weight, height:\n");
scanf("%d,%d",&x,&y);
printf("weight=%d\n",x*2);
A=(float)y/100;
printf("height=%.2f\n",A);
b=(float)x/(A*A);
printf("t=%.2f\n",b);
}
第4周
1
数位拆分v2.0(4分)
题目内容:
从键盘上输入一个4位数的整数n,编写程序将其拆分为两个2位数的整数a和b,计算并输出拆分后的两个数的加、减、乘、除和求余运算的结果。例如n=-4321,设拆分后的两个整数为a,b,则a=-43,b=-21。除法运算结果要求精确到小数点后2位。求余和除法运算需要考虑除数为0的情况,即如果拆分后b=0,则输出提示信息"The second operater is zero!"
程序的运行结果示例1:
Please input n:
1200↙
12,0
sum=12,sub=12,multi=0
The second operater is zero!
程序的运行结果示例2:
Please input n:
-2304↙
-23,-4
sum=-27,sub=-19,multi=92
dev=5.75,mod=-3
输入格式: "%d"
输出格式:
输入提示信息:"Please input n:\n"
拆分后的两个整数的输出格式:"%d,%d\n"
加法、减法、乘法的输出格式:"sum=%d,sub=%d,multi=%d\n"
除法和求余的输出格式:"dev=%.2f,mod=%d\n"
除数为0的提示信息:"The second operater is zero!\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h> <math.h>
main()
{
float c;
int x,b0,b1,b2,b3,a,b,d;
printf("Please input n:\n");
scanf("%d",&x);
b3 = x/1000; /*取x千位*/
b2 = (x-b3*1000)/100; /*取x百位*/
b1 = (x-b3*1000-b2*100)/10; /*取x十位*/
b0 = x%10; /*取x个位*/
a=b3*10+b2; /*拆分输入的整数为两段*/
b=b1*10+b0;
printf("%d,%d\n",a,b);
printf("sum=%d,sub=%d,multi=%d\n",a+b,a-b,a*b); /*输出两段数的加,减,乘*/
if(0==b)
{
printf("The second operater is zero!\n");
}
else
{
c=(float)a/b;
d=a%b;
printf("dev=%.2f,mod=%d\n",c,d);
}
return 0;
}
2
快递费用计算(4分)
题目内容:
上海市的某快递公司根据投送目的地距离公司的远近,将全国划分成5个区域:
0区
1区
2区
3区
4区
同城
临近两省
1500公里(含)以内
1500——2500公里
2500公里以上
上海
江苏,浙江
北京,天津,河北,辽宁,河南,安微,陕西,湖北,江西,湖南,福建,广东,山西。
吉林,辽宁,甘肃,四川,重庆,青海,广西,云南,海南,内蒙古,黑龙江,贵州。
新疆,西藏。
快递费按邮件重量计算,由起重费用、续重费用两部分构成:
(1) 起重(首重)1公斤按起重资费计算(不足1公斤,按1公斤计算),超过首重的重量,按公斤(不足1公斤,按1公斤计算)收取续重费;
(2) 同城起重资费10元,续重3元/公斤;
(3) 寄往1区(江浙两省)的邮件,起重资费10元,续重4元;
(4) 寄往其他地区的邮件,起重资费统一为15元。而续重部分,不同区域价格不同:2区的续重5元/公斤,3区的续重6.5元/公斤,4区的续重10元/公斤。
编写程序,从键盘输入邮件的目的区域编码和重量,计算并输出运费,计算结果保留2位小数。
提示:续重部分不足一公斤,按1公斤计算。因此,如包裹重量2.3公斤:1公斤算起重,剩余的1.3公斤算续重,不足1公斤按1公斤计算,1.3公斤折合续重为2公斤。如果重量应大于0、区域编号不能超出0-4的范围。
程序运行结果示例1:
4,4.5↙
Price: 55.00
程序运行结果示例2:
5,3.2↙
Error in Area
Price: 0.00
输入格式:
用逗号分隔的两个数字,第一个表示区域、第二个是重量:"%d,%f"
输出格式:
价格的输出格式:"Price: %5.2f\n"
区域错误的提示信息:"Error in Area\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
#include <math.h>
#define eps 1E-5
main()
{
float b,c,d;
int a;
scanf("%d,%f",&a,&b);
switch(a)
{
case 0:
if (b<=1+eps)
{
c=10.0;
}
else
{
d=ceil(b-1.0);
c=d*3+10.0;
}
printf("Price: %5.2f\n",c);
break;
case 1:
if (b<=1+eps)
{
c=10.0;
}
else
{
d=ceil(b-1.0);
c=d*4+10.0;
}
printf("Price: %5.2f\n",c);
break;
case 2:
if (b<=1+eps)
{
c=15.0;
}
else
{
d=ceil(b-1.0);
c=d*5+15.0;
}
printf("Price: %5.2f\n",c);
break;
case 3:
if (b<=1+eps)
{
c=15.0;
}
else
{
d=ceil(b-1.0);
c=d*6.5+15;
}
printf("Price: %5.2f\n",c);
break;
case 4:
if (b<=1+eps)
{
c=15.0;
}
else
{
d=ceil(b-1.0);
c=d*10+15.0;
}
printf("Price: %5.2f\n",c);
break;
default:
printf("Error in Area\n");
printf("Price: 0.00");
}
return 0;
}
3
数据区间判断(6分)
题目内容:
从键盘输入一个int型的正整数n(已知:0<n<10000),编写程序判断n落在哪个区间。如果用户输入的数据不在指定的范围里,程序输出 "error!"。例如,输入265,则该数属于区间 100-999。
程序运行结果示例1:
Please enter the number:
2563↙
2563: 1000-9999
程序运行结果示例2:
Please enter the number:
156↙
156: 100-999
程序运行结果示例3:
Please enter the number:
36↙
36: 10-99
程序运行结果示例4:
Please enter the number:
3↙
3: 0-9
程序运行结果示例5:
Please enter the number:
10923↙
error!
输入格式: "%d"
输出格式:
输入提示信息:"Please enter the number:\n"
输出的区间判断:
"%d: 1000-9999\n"
"%d: 100-999\n"
"%d: 10-99\n"
"%d: 0-9\n"
输入错误提示信息:"error!\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
#include <math.h>
int main()
{
int x,b0,b1,b2,b3,y,a,b,c;
printf("Please enter the number:\n");
scanf("%d",&x);
if (x<=0||x>=10000)
{
printf("error!\n");
}
else
{
b3 = x/1000; /*取x千位*/
b2 = (x-b3*1000)/100; /*取x百位*/
b1 = (x-b3*1000-b2*100)/10; /*取x十位*/
b0 = x%10; /*取x个位*/
if(b3!=0)
{
printf("%d: 1000-9999\n",x);
}
else if(b3==0&&b2!=0)
{
printf("%d: 100-999\n",x);
}
else if(b3==0&&b2==0&&b1!=0)
{
printf("%d: 10-99\n",x);
}
else
{
printf("%d: 0-9\n",x);
}
}
return 0;
}
4
计算一元二次方程的根v2.0(4分)
题目内容:
根据下面给出的求根公式,计算并输出一元二次方程的两个实根,要求精确到小数点后4位。其中a,b,c的值由用户从键盘输入。如果用户输入的系数不满足求实根的要求,输出错误提示 "error!"。
程序运行结果示例1:
Please enter the coefficients a,b,c:
1,2,1↙
x1=-1.0000, x2=-1.0000
程序运行结果示例2:
Please enter the coefficients a,b,c:
2,6,1↙
x1=-0.1771, x2=-2.8229
程序运行结果示例3:
Please enter the coefficients a,b,c:
2,1,6↙
error!
输入格式: "%f,%f,%f"
输出格式:
输入提示信息:"Please enter the coefficients a,b,c:\n"
输出格式:"x1=%7.4f, x2=%7.4f\n"
输入错误提示信息:"error!\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
#include <math.h>
int main()
{
float a,b,c,disc,p,q;
printf("Please enter the coefficients a,b,c:\n");
scanf("%f,%f,%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc<0)
{
printf("error!\n");
}
else
{
p=-b/(2*a);
q=sqrt(disc)/(2*a);
printf("x1=%7.4f, x2=%7.4f\n",q+p,p-q);
}
return 0;
}
第五周
1
6位密码输入检测(4分)
题目内容:
从键盘输入6位仅由数字0~9组成的密码。用户每输入一个密码并按回车键后,程序给出判断:如果是数字,则原样输出该数字,并提示用户目前已经输入了几位密码,同时继续输入下一位密码;否则,程序提示"error",并让用户继续输入下一位密码。直到用户输入的密码全部是数字为止。
以下为程序的运行结果示例:
Input your password:
1↙
1, you have enter 1-bits number
6↙
6, you have enter 2-bits number
a↙
error
d↙
error
4↙
4, you have enter 3-bits number
6↙
6, you have enter 4-bits number
8↙
8, you have enter 5-bits number
2↙
2, you have enter 6-bits number
输入格式:
数字字符输入格式:"%c"
输出格式:
输入提示信息:"Input your password:\n"
如果输入的是数字,输出格式为:"%c, you have enter %d-bits number\n"
如果输入的不是数字,输出提示信息:"error\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
int main()
{
int i;
char c;
printf("Input your password:\n");
i=0;
while (i<6)
{
scanf("%c%*c",&c);
if ((c>=48)&&(c<=57))
{
i=i+1;
printf("%c, you have enter %d-bits number\n",c,i);
}
else
{
printf("error\n");
}
}
return 0;
}
2
判断一个整型数据有几位v1.0(4分)
题目内容:
从键盘输入一个整型数据(int型),编写程序判断该整数共有几位。例如,从键盘输入整数16644,该整数共有5位。
程序运行结果示例1:
Please enter the number:
21125↙
21125: 5 bits
程序运行结果示例2:
Please enter the number:
-12234↙
-12234: 5 bits
输入格式: "%d"
输出格式:
输入提示信息:"Please enter the number:\n"
判断该整数共有几位: "%d: %d bits\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
#include <math.h>
int main()
{
int n,nn,b,i,j,sum;
printf("Please enter the number:\n");
scanf("%d",&n);
nn=fabs(n);
b=1;
sum=1;
i=1;
j=10;
while (b=1)
{
if ((nn>=i)&&(nn<j))
{
printf("%d: %d bits\n",n,sum);
b=0;
break;
}
else
{
i=i*10;
j=j*10;
sum=sum+1;
}
}
return 0;
}
3
检测输入数据中奇数和偶数的个数(4分)
题目内容:
从键盘输入一系列正整数,输入-1表示输入结束(-1本身不是输入的数据)。编写程序判断输入数据中奇数和偶数的个数。如果用户输入的第一个数据就是-1,则程序输出"over!"。否则。用户每输入一个数据,输出该数据是奇数还是偶数,直到用户输入-1为止,分别统计用户输入数据中奇数和偶数的个数。
程序运行结果示例1:
Please enter the number:
1↙
1:odd
5↙
5:odd
8↙
8:even
9↙
9:odd
12↙
12:even
17↙
17:odd
-1↙
The total number of odd is 4
The total number of even is 2
程序运行结果示例2:
Please enter the number:
-1↙
over!
The total number of odd is 0
The total number of even is 0
输入格式: "%d"
输出格式:
输入提示信息:"Please enter the number:\n"
用户输入的第一个数据就是-1,输出格式:"over!\n"
奇数的输出格式:"%d:odd\n"
偶数的输出格式:"%d:even\n"
输入数据中奇数的个数统计:"The total number of odd is %d\n"
输入数据中偶数的个数统计:"The total number of even is %d\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
int main()
{
int a,odd,even;
odd=0;even=0;
printf("Please enter the number:\n");
a=0;
while (a!=-1)
{
scanf("%d",&a);
if (a!=-1)
{
if (a%2==1)
{
odd++;
printf("%d:odd\n",a);
}
else {
even++;
printf("%d:even\n",a);
}
}
}
if (even+odd==0)
{
printf("over!\n");
}
printf("The total number of odd is %d\n",odd);
printf("The total number of even is %d\n",even);
return 0;
}
4
计算球的反弹高度(4分)
题目内容:
一个球从100米高度自由落下,每次落地后反跳回原高度的一半,再落下并反弹......,求它在第5次和第10次落地时,分别共经过了多少米?第5次和第10次反弹分别是多高?要求计算结果保留到小数点后3位。用户从键盘输入想要计算的第n次(n<=15)。
程序运行结果示例1:
Input:
5↙
5 times:
287.500
3.125
程序运行结果示例2:
Input:
10↙
10 times:
299.609
0.098
输入格式: "%d"
输出格式:
反弹次数:"%d times:\n"
第n次反弹共经过多少米:"%.3f\n"
第n次的反弹高度:"%.3f\n"
输入提示信息:"Input:\n"
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include <stdio.h>
int main()
{
double h,sum=0;
int n,i;
printf("Input:\n");
scanf("%d",&n);
h=100;
for (i=1;i<=n;i++)
{
sum+=h;
h/=2;
sum+=h;
}sum-=h;
printf("%d times:\n",n);
printf("%.3f\n",sum);
printf("%.3f\n",h);
return 0;
}
第六周
1
程序改错v2.0(5分)
下面代码的功能是将百分制成绩转换为5分制成绩,具体功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。
1. #include<stdio.h>
2. int main()
3. {
4. int score;
5. char grade;
6. printf("Please input score:");
7. scanf("%d", &score);
8. if (score < 0 || score > 100)
9. printf("Input error!\n");
10. else if (score >= 90)
11. grade = 'A’;
12. else if (score >= 80)
13. grade = 'B';
14. else if (score >= 70)
15. grade = 'C';
16. else if (score >= 60)
17. grade = 'D';
18. else
19. grade = 'E';
20. printf("grade:%c\n", grade);
21. return 0;
22. }
程序运行结果示例1:
Please input score:
a↙
Input error!
Please input score:
-12↙
Input error!
Please input score:
230↙
Input error!
Please input score:
92↙
grade: A
程序运行结果示例2:
Please input score:
88↙
grade: B
程序运行结果示例3:
Please input score:
73↙
grade: C
程序运行结果示例4:
Please input score:
65↙
grade: D
程序运行结果示例5:
Please input score:
27↙
grade: E
输入提示信息:"Please input score:\n"
输入格式: "%d"
输出格式:
输入错误时的提示信息:"Input error!\n"
输出格式:"grade: %c\n" (注意:%c前面有一个空格)
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
时间限制:500ms内存限制:32000kb
#include<stdio.h>
int main()
{
int score=-1;
char grade;
printf("Please input score:\n");
scanf("%d", &score);
while(score < 0 || score > 100)
{
getchar();
展开阅读全文