编程基础题-4

来源:互联网 发布:@Requestbody解析json 编辑:程序博客网 时间:2024/05/21 17:31

2017/05/15

题目10:一球从100米高度自由落下,每次落地后反跳回原高度的一半;再落下,求它在 第10次落地时,共经过多少米?第10次反弹多高?

分析
 计算每一次落地时的距离,第i次反弹,变成1/(i+1)
解答

public class chapter10 {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("输入球掉落的高度:");        float high = input.nextFloat();        System.out.print("输入落地的次数:");        int number = input.nextInt();        get_result(high,number);    }    private static void get_result(float high, int number) {        float result_high = high;        float distance = high;        for (int i = 1 ; i < number ; i++) {            result_high /= 2.0;            distance += result_high * 2.0;        }        result_high /= 2.0;        System.out.println("第"+number+"次落地共经过"+distance+"米!");        System.out.println("第"+number+"次落地反弹"+result_high+"米!");    }}

结果:

输入球掉落的高度:100输入落地的次数:3第3次落地共经过250.0米!第3次落地反弹12.5米!

心得
 注意这里要用float作为高度的数据类型。

===========================================================================

题目11:有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?

分析
 简单
解答

public class chapter10 {    public static void main(String[] args) {        Scanner input = new Scanner(System.in);        System.out.print("输入球掉落的高度:");        float high = input.nextFloat();        System.out.print("输入落地的次数:");        int number = input.nextInt();        get_result(high,number);    }    private static void get_result(float high, int number) {        float result_high = high;        float distance = high;        for (int i = 1 ; i < number ; i++) {            result_high /= 2.0;            distance += result_high * 2.0;        }        result_high /= 2.0;        System.out.println("第"+number+"次落地共经过"+distance+"米!");        System.out.println("第"+number+"次落地反弹"+result_high+"米!");    }}

结果:

123是这种数!124是这种数!132是这种数!134是这种数!142是这种数!143是这种数!213是这种数!214是这种数!231是这种数!234是这种数!241是这种数!243是这种数!312是这种数!314是这种数!321是这种数!324是这种数!341是这种数!342是这种数!412是这种数!413是这种数!421是这种数!423是这种数!431是这种数!432是这种数!共有24个数!

心得
 这里的条件注意要有三个不等式都成立才行。

===========================================================================

题目12:题目:企业发放的奖金根据利润提成。利润(I)低于或等于10万元时,奖金可提10%;利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可可提成7.5%;20万到40万之间时,高于20万元的部分,可提成5%;40万到60万之间时高于40万元的部分,可提成3%;60万到100万之间时,高于60万元的部分,可提成1.5%,高于100万元时,超过100万元的部分按1%提成,从键盘输入当月利润I,求应发放奖金总数? 

分析
 分段讨论一下即可
解答

public class chapter12 {    public static void main(String[] args) {        System.out.println("请输入当月利润(万):");        Scanner input = new Scanner(System.in);        long profit = input.nextLong();        System.out.print("应该发放的奖金总数为:");        double reward = get_reward(profit);        System.out.println(reward+"万!");    }    private static double get_reward(long profit) {        double reward = 0;        if(profit > 100){            reward  = get_reward(100) + 0.01*(profit-100);        }        else if(profit >60){            reward = get_reward(60)+0.015*(profit - 60);        }        else if(profit > 40){            reward = get_reward(40)+ 0.03*(profit - 40);        }        else if(profit > 20){            reward = get_reward(20)+0.05*(profit - 20);        }        else if(profit > 10){            reward = get_reward(10) + 0.075*(profit - 10);        }        else if (profit >= 0){            reward = profit * 0.1;        }        else            System.out.println("input error!");        return reward;    }

结果:

请输入当月利润(万):50应该发放的奖金总数为:3.05万!

心得
 注意数据类型的使用
参考答案

public class Prog12{    public static void main(String[] args){        System.out.print("请输入当前利润:");        long profit = Long.parseLong(key_Input());        System.out.println("应发奖金:"+bonus(profit));    }    //接受从键盘输入的内容    private static String key_Input(){        String str = null;        BufferedReader bufIn = new BufferedReader(new InputStreamReader(System.in));        try{            str = bufIn.readLine();        }catch(IOException e){            e.printStackTrace();        }finally{            try{                bufIn.close();            }catch(IOException e){                e.printStackTrace();            }        }        return str;    }    //计算奖金    private static long bonus(long profit){        long prize = 0;        long profit_sub = profit;        if(profit>1000000){            profit = profit_sub-1000000;            profit_sub = 1000000;            prize += profit*0.01;        }        if(profit>600000){            profit = profit_sub-600000;            profit_sub = 600000;            prize += profit*0.015;         }        if(profit>400000){            profit = profit_sub-400000;            profit_sub = 400000;            prize += profit*0.03;        }        if(profit>200000){            profit = profit_sub-200000;            profit_sub = 200000;            prize += prize*0.05;        }        if(profit>100000){            profit = profit_sub-100000;            profit_sub = 100000;            prize += profit*0.075;        }        prize += profit_sub*0.1;        return prize;    }}
1 0
原创粉丝点击