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

问题 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 = 100000 * (0.1 + 0.075) + (400000 - 200000)*0.05 + (profit - 400000) * 0.03;
    }else if(profit > 600000 && profit <= 1000000){
        bonus = 100000 * (0.1 + 0.075) + (400000 - 200000)*0.05+ (600000 - 400000) * 0.03 + (profit - 600000) * 0.015;
    }else{
        bonus = 100000 * (0.1 + 0.075) + (400000 - 200000)*0.05+ (600000 - 400000) * 0.03 + (1000000 - 600000) * 0.015 + (profit - 600000) * 0.01;
    }
    printf("%.2f", bonus);
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值