《算法笔记》——基础篇习题选择结构

条件语句锻炼逻辑思维能力

第二章 C/C++快速入门——2.3选择结构

【习题A】 一元二次方程求根

Problem Description

求一元二次方程ax2+bx+c=0的根,三个系数a, b, c由键盘输入,且a不能为0,但不保证b2-4ac>0。
程序中所涉及的变量均为double类型。

Input
以空格分隔的一元二次方程的三个系数,双精度double类型

Output
分行输出两个根如下(注意末尾的换行):
r1=第一个根
r2=第二个根
结果输出时,宽度占7位,其中小数部分2位。
如果方程无实根,输出一行如下信息(注意末尾的换行):
No real roots!

Sample Input
1 2 3

Sample Output
No real roots!

Thinking Notes

题目不保证delt>0,所以需要条件判断

Code Implementation(C)

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

【习题B】 比较交换实数值

Problem Description

从键盘输入2个实数,按照代数值由小到大的顺序输出这两个数。

Input
用空格分隔的两个实数。

Output
从小到大输出这两个实数,中间以空格来分隔,小数在前,大数在后。
小数点后保留2位小数。
末尾输出换行符。

Sample Input
3.6 -2.3

Sample Output
-2.30 3.60

Thinking Notes

swap函数头文件#include

Code Implementation(C)

#include <cstdio>
#include <algorithm>
using namespace std;
int main(){
    double a,b;
    scanf("%lf %lf",&a,&b);
    if(a>b){
        swap(a,b);
    }
    printf("%.2f %.2f\n",a,b);
    return 0;
}

【习题C】 比较交换3个实数值,并按序输出

Problem Description

从键盘输入3个实数a, b, c,通过比较交换,将最小值存储在变量a中,最大值存储在变量c中,中间值存储在变量b中,并按照从小到大的顺序输出这三个数a, b, c。
末尾输出换行。

Input
输入以空格分隔的三个实数

Output
按照从小到大的顺序输出这三个实数,中间以空格分隔,最小值在前,最大值在后。小数点后保留2位小数。
注意末尾的换行。

Sample Input
3 7 1

Sample Output
1.00 3.00 7.00

Code Implementation(C)

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

【习题D】 三个整数求最大值

Problem Description

有3个整数a, b, c,由键盘输入,输出其中最大的数。

Input
以空格分割的三个整数。

Output
三个数中的最大值,末尾换行。

Sample Input
1 3 2

Sample Output
3

Code Implementation(C)

#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(a<b){
    	if(a>c){
    		printf("%d\n",b);
    	}
    	else{
    		if(b>c){
    			printf("%d\n",b);
    		}
    		else{
    			printf("%d\n",c);
    		}
    	}
    }
    return 0;
}

【习题E】 奖金计算

Problem Description

某企业发放的奖金根据利润提成。利润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语句实现。

Input
企业利润,小数,双精度double类型

Output
应发奖金数,保留2位小数,末尾换行。

Sample Input
1050

Sample Output
105.00

Code Implementation(C)

#include<stdio.h>
int main(){
    double l,m;
    scanf("%lf",&l);
    if(l<=100000){
        m=0.1*l;
    }
    else if(l<=200000 && l>100000){
        m=100000*0.1+(l-100000)*0.075;
    }
    else if(l<=400000 && l>200000){
        m=100000*0.1+100000*0.075+(l-200000)*0.05;
    }
    else if(l<=600000 && l>400000){
        m=100000*0.1+100000*0.075+200000*0.05+(l-400000)*0.03;
    }
    else if(l<=1000000 && l>600000){
        m=100000*0.1+100000*0.075+200000*0.05+200000*0.03+(l-600000)*0.015;
    }
    else if(l>1000000){
        m=100000*0.1+100000*0.075+200000*0.05+200000*0.03+400000*0.015+(l-1000000)*0.01;
    }
    printf("%.2f\n",m);
    return 0;
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值