《算法笔记》2.3小节——C/C++快速入门->选择结构
第一题:例题4-1 一元二次方程求根
题目描述
求一元二次方程ax2+bx+c=0的根,三个系数a, b, c由键盘输入,且a不能为0,但不保证b2-4ac>0。
程序中所涉及的变量均为double类型。
输入
以空格分隔的一元二次方程的三个系数,双精度double类型
输出
分行输出两个根如下(注意末尾的换行):
r1=第一个根
r2=第二个根
结果输出时,宽度占7位,其中小数部分2位。
如果方程无实根,输出一行如下信息(注意末尾的换行):
No real roots!
样例输入 Copy
1 2 3
样例输出 Copy
No real roots!
#include <stdio.h>
#include <math.h>
int main() {
double a, b, c, r1, r2;
scanf("%lf%lf%lf", &a, &b, &c);
double discriminant;
discriminant = b * b - 4 * a * c;
if (a) {
if (discriminant >= 0) {
r1 = (-b + sqrt(discriminant)) / (2 * a);
r2 = (-b - sqrt(discriminant)) / (2 * a);
printf("r1=%7.2f\nr2=%7.2f", r1, r2);
} else {
printf("No real roots!");
}
}
return 0;
}
第二题:例题4-2 比较交换实数值
题目描述
从键盘输入2个实数,按照代数值由小到大的顺序输出这两个数。
输入
用空格分隔的两个实数。
输出
从小到大输出这两个实数,中间以空格来分隔,小数在前,大数在后。
小数点后保留2位小数。
末尾输出换行符。
样例输入 Copy
3.6 -2.3
样例输出 Copy
-2.30 3.60
三目为王
#include <stdio.h>
int main() {
double a, b;
scanf("%lf%lf", &a, &b);
a > b ? printf("%.2f %.2f\n", b, a) : printf("%.2f %.2f\n", a, b);
return 0;
}
通俗易懂
#include <stdio.h>
int main()
{
double a,b;
scanf("%lf%lf",&a,&b);
if(a>b)
{
printf("%.2f %.2f\n",b,a);
}
else
{
printf("%.2f %.2f\n",a,b);
}
return 0;
}
第三题:例题4-3 比较交换3个实数值,并按序输出
题目描述
从键盘输入3个实数a, b, c,通过比较交换,将最小值存储在变量a中,最大值存储在变量c中,中间值存储在变量b中,并按照从小到大的顺序输出这三个数a, b, c。
末尾输出换行。
输入
输入以空格分隔的三个实数
输出
按照从小到大的顺序输出这三个实数,中间以空格分隔,最小值在前,最大值在后。小数点后保留2位小数。
注意末尾的换行。
样例输入 Copy
3 7 1
样例输出 Copy
1.00 3.00 7.00
#include <stdio.h>
int main() {
double a, b, c;
scanf("%lf%lf%lf", &a, &b, &c);
if (a > b) {
if (b > c) {
printf("%.2f %.2f %.2f", c, b, a);
} else {
if (a > c) {
printf("%.2f %.2f %.2f", b, c, a);
} else {
printf("%.2f %.2f %.2f", b, a, c);
}
}
} else {
if (c > b) {
printf("%.2f %.2f %.2f", a, b, c);
} else {
if (a > c) {
printf("%.2f %.2f %.2f", c, a, b);
} else {
printf("%.2f %.2f %.2f", a, c, b);
}
}
}
return 0;
}
第四题:习题4-4 三个整数求最大值
题目描述
有3个整数a, b, c,由键盘输入,输出其中最大的数。
输入
以空格分割的三个整数。
输出
三个数中的最大值,末尾换行。
样例输入 Copy
1 3 2
样例输出 Copy
3
又是可爱的三目
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
a > b ? (a > c ? printf("%d", a) : printf("%d", c)) : (b > c ? printf("%d", b) : printf("%d", c));
return 0;
}
通俗易懂
#include <stdio.h>
int main() {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
if (a > b) {
if (b > c) {
printf("%d\n", a);
} else {
if (a > c)
printf("%d\n", a);
else
printf("%d\n", c);
}
} else {
if (b > c) {
if (a > c)
printf("%d\n", b);
else
printf("%d\n", b);
} else {
printf("%d\n", c);
}
}
return 0;
}
第五题:习题4-10-1 奖金计算
题目描述
某企业发放的奖金根据利润提成。利润I低于或等于100000时,奖金可提10%;利润高于100000元,低于200000元(100000<I<=200000)时,低于100000元的部分仍按10%提成,高于100000元的部分提成比例为7.5%;200000<I<=400000时,低于200000元的部分仍按上述方法提成(下同),高于200000元的部分按5%提成;400000<I<=600000元时,高于400000元的部分按3%提成;600000<I<=1000000时,高于600000元的部分按1.5%提成;I>1000000元时,超过1000000元的部分按1%提成。
从键盘输出当月利润I,求应发奖金数,奖金精确到分。
要求用if语句实现。
输入
企业利润,小数,双精度double类型
输出
应发奖金数,保留2位小数,末尾换行。
样例输入 Copy
1050
样例输出 Copy
105.00
#include <stdio.h>
int main() {
double profit, bonus;
scanf("%lf", &profit);
if (profit <= 100000)
bonus = profit * 0.1;
else if (profit <= 200000)
bonus = (profit - 100000) * 0.075 + 10000;
else if (profit <= 400000)
bonus = (profit - 200000) * 0.05 + 7500 + 10000;
else if (profit <= 600000)
bonus = (profit - 400000) * 0.03 + 10000 + 7500 + 10000;
else if (profit <= 1000000)
bonus = (profit - 600000) * 0.015 + 6000 + 10000 + 7500 + 10000;
else
bonus = (profit - 1000000) * 0.01 + 3000 + 6000 + 10000 + 7500 + 10000;
printf("%.2f", bonus);
return 0;
}