1输出逆序数(3分)
题目内容:
从键盘任意输入一个3位整数,编程计算并输出它的逆序数(忽略整数前的正负号)。例如,输入-123,则忽略负号,由123分离出其百位1、十位2、个位3,然后计算3100+210+1 = 321,并输出321。
提示:
-
从键盘输入数据可以使用函数scanf()。例如,scanf("%d", &x); 表示从键盘输入整数并存入整形变量x中。
-
利用取绝对值函数 fabs()忽略输入数据的负号。fabs(x)表示计算变量x的绝对值。
3.使用数学函数,需要在程序开头加上编译预处理指令 #include <math.h>
以下为程序的一个运行示例
Input x:
-123↙
y=321
输入格式: “%d”
输出格式:
提示信息:“Input x:\n”
输出格式:“y=%d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int x,y;
printf("Input x:\n");
scanf("%d",&x);
x=abs(x);
y=x/100+10*(x/10%10)+100*(x%10);
printf("y=%d\n",y);
}
2计算总分和平均分(3分)
题目内容:
小明本学期共有5门课程,分别是英语、语文、数学、历史和音乐。5科的期中考试成绩分别是86分、74分、92分、77分、82分,期末考试成绩分别是81分、87分、90分、62分、88分。已知期中和期末考试成绩分别占总成绩的30%和70%。定义相应的变量存放各科成绩,并计算出小明5门课程的总分和平均分。要求平均分输出两种形式:带2位小数的浮点数形式和不带小数的整数形式。要求总分输出带2位小数的浮点数形式。程序中浮点数的数据类型均为float类型。
提示:
输出不带小数的平均分的整数形式可以使用强制类型转换。
以下为输出示例:
total=408.90
average=81.78
average=81
输入格式:无
输出格式:
输出总分的格式: “total=%.2f\n”
输出平均分的格式: “average=%.2f\n”
“average=%d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
//英语、语文、数学、历史和音乐
int e1=86,c1=74,m1=92,h1=77,mu1=82;
int e2=81,c2=87,m2=90,h2=62,mu2=88;
float total,average;
total=(e1+c1+m1+h1+mu1)*0.3+(e2+c2+m2+h2+mu2)*0.7;
average = total/5;
printf("total=%.2f\n",total);
printf("average=%.2f\n",average);
printf("average=%d\n",(int)average);
}
3存款利率计算器V1.0(3分)
题目内容:
设银行定期存款的年利率rate为2.25%,已知存款期为n年,存款本金为capital元,试编程计算并输出n年后的本利之和deposit。程序中所有浮点数的数据类型均为double类型。
提示:
-
从键盘输入数据可以使用函数scanf()。本例中为scanf("%lf,%d,%lf", &rate, &n, &capital);
-
本程序最终计算的是复利。
-
计算幂的数学函数为pow(a,n), 代表a的n次幂。
-
使用数学函数,需要在程序开头加上编译预处理指令 #include <math.h>
以下为程序的一个运行示例:
Please enter rate, year, capital:
0.0225,10,1000↙
deposit=1249.203
输入格式: “%lf,%d,%lf”
输出格式:
输入信息提示: “Please enter rate, year, capital:\n”
输出格式:“deposit=%.3f\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
double rate,capital;
int year;
printf("Please enter rate, year, capital:\n");
scanf("%lf,%d,%lf",&rate,&year,&capital);
printf("deposit=%.3f\n",capital*pow((1+rate),year));
}
4数位拆分v1.0(3分)
题目内容:
现有一个4位数的正整数n=4321(即n是一个已知的数,固定为4321),编写程序将其拆分为两个2位数的正整数43和21,计算并输出拆分后的两个数的加、减、乘、除和求余的结果。例如n=4321,设拆分后的两个整数为a,b,则a=43,b=21。除法运算结果要求精确到小数点后2位,数据类型为float。
以下为程序的运行结果示例:
a=43,b=21
a+b=64
a-b=22
a*b=903
a/b=2.05
a%b=1
输入格式: 无
输出格式:
数位拆分的输出格式:“a=%d,b=%d\n”
加法运算的输出格式:“a+b=%d\n”
减法运算的输出格式:“a-b=%d\n”
乘法运算的输出格式:“a*b=%d\n”
除法运算的输出格式:“a/b=%.2f\n”
求余运算的输出格式:“a%%b=%d\n”
为避免出现格式错误,请直接拷贝粘贴题目中给的格式字符串和提示信息到你的程序中。
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int data=4321,a,b;
a = data / 100;
b = data % 100;
printf("a=%d,b=%d\n",a,b);
printf("a+b=%d\n",a+b);
printf("a-b=%d\n",a-b);
printf("a*b=%d\n",a*b);
printf("a/b=%.2f\n",a*1.0/b);
printf("a%%b=%d\n",a%b);
}
5求正/负余数(3分)
题目内容:
在C语言中,如果被除数为负值,则对一个正数求余的时候,求出的余数也是一个负数。在某些场合下,我们需要求出它的正余数,例如:在C语言中有(-11)%5=-1,但是有时我们希望得到的余数不是-1,而是4。请编写程序计算(-11)%5的负余数和正余数。
以下为程序运行结果示例:
negative: -1
positive: 4
输入格式:无
输出格式:
负余数的输出格式:“negative: %d\n”
正余数的输出格式:“positive: %d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int a=-11,b=5;
printf("negative: %d\n",a%b);
printf("positive: %d\n",a%b+b);
}
6身高预测(3分)
题目内容:
已知小明(男孩)爸爸的身高是175厘米,妈妈的身高是162厘米。小红(女孩)爸爸的身高是169厘米,妈妈的身高是153厘米,按照下面公式,预测并输出小明和小红的遗传身高(不考虑后天因素)。
男性成人时身高=(faHeight + moHeight)×0.54cm
女性成人时身高=(faHeight×0.923 + moHeight)/2cm
以下为程序的运行结果示例:
Height of xiao ming:181
Height of xiao hong:154
输入格式: 无
输出格式:
小明身高的输出格式:“Height of xiao ming:%d\n”
小红身高的输出格式:“Height of xiao hong:%d\n”
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int mingfa=175,mingma=162;
int hongfa=169,hongma=153;
printf("Height of xiao ming:%d\n",(int)((mingfa + mingma)*0.54));
printf("Height of xiao hong:%d\n",(int)((hongfa*0.923 + hongma)/2));
}
求一元二次方程的根(3分)
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int a=2,b=3,c=1;
float x1,x2;
x1=(sqrt(b*b-4*a*c)-b)/(2*a);
x2=(-sqrt(b*b-4*a*c)-b)/(2*a);
printf("x1=%.4f\n",x1);
printf("x2=%.4f\n",x2);
}