当前位置: 代码迷 >> 综合 >> 《算法笔记》第二章 C/C++快速入门 第2.2小节 选择结构
  详细解决方案

《算法笔记》第二章 C/C++快速入门 第2.2小节 选择结构

热度:11   发布时间:2024-02-13 04:30:45.0

问题 A: 例题4-1 一元二次方程求根

#include <stdio.h>
#include <stdlib.h>
#include <math.h>int main()
{double a,b,c,r1,r2,temp;scanf("%lf%lf%lf", &a,&b,&c);temp = pow(b, 2.0) - 4*a*c;if(a){if(temp < 0){printf("No real roots!");}else{r1 = (-b + sqrt(temp)) / 2*a;r2 = (-b - sqrt(temp)) / 2*a;printf("r1=%7.2f\n", r1);printf("r2=%7.2f", r2);}}return 0;
}

问题 B: 例题4-2 比较交换实数值

#include <stdio.h>
#include <stdlib.h>int main()
{double num1, num2;scanf("%lf%lf", &num1, &num2);if(num1 >= num2){printf("%.2f %.2f\n", num2, num1);}else{printf("%.2f %.2f\n", num1, num2);}return 0;
}

问题 C: 例题4-3 比较交换3个实数值,并按序输出

#include <stdio.h>
#include <stdlib.h>int main()
{double a,b,c,temp;scanf("%lf%lf%lf", &a,&b,&c);if(a>=b){temp = a;a = b;b= temp;}if(b>=c){temp = b;b = c;c = temp;if(a >= b){temp = a;a = b;b = temp;}}printf("%.2f %.2f %.2f\n", a,b,c);return 0;
}

问题 D: 习题4-4 三个整数求最大值

#include <stdio.h>
#include <stdlib.h>int main()
{int a,b,c,temp;scanf("%d%d%d", &a, &b, &c);if(a>=b){temp = a;}else{temp = b;}if(temp >= c){printf("%d", temp);}else{printf("%d", c);}return 0;
}

问题 E: 习题4-10-1 奖金计算

#include <stdio.h>
#include <stdlib.h>int main()
{//profit: 公司利润  bonus: 应得奖金double profit, bonus;scanf("%lf", &profit);if(profit >=0 && profit <= 100000){bonus = profit * 0.1;}else if (profit > 100000 && profit <= 200000){bonus = 100000 * 0.1 + (profit - 100000) * 0.075;}else if (profit > 200000 && profit <= 400000){bonus = 100000 * (0.1+ 0.075) + (profit - 200000) * 0.05;}else if(profit > 400000 && profit <= 600000){bonus = 10000 * (0.1 + 0.075 + 0.05) + (profit - 400000) * 0.03;}else if(profit > 600000 && profit <= 1000000){bonus = 10000 * (0.1 + 0.075 + 0.05 + 0.03) + (profit - 600000) * 0.015;}else{bonus = 10000 * (0.1 + 0.075 + 0.05 + 0.03 + 0.015) + (profit - 600000) * 0.01;}printf("%.2f", bonus);return 0;
}

 

  相关解决方案