Java期末(一)

公告

本题库将分为两篇发,第一篇发编程题,第二篇发其他题目,如果发现答案错误情况请及时反馈
题库为同学提供,所以这里不做担保

第一题

有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问每个月的兔子对数为多少?

import java.util.Scanner;

/**
 * @author Accepted
 * @create 2021/12/19 12:21
 * 兔子问题
 */
public class Test01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int months = sc.nextInt();
        int x = 1, t, n = 0;
        for (int i = 1; i < months; i++) {
            t = x;
            x = x + n;
            n = t;
        }

        System.out.println("第" + months + "月有" + x + "对兔子");
    }
}

第二题

判断101-200之间有多少个素数,并输出所有素数。

/**
 * @author Accepted
 * @create 2021/12/19 12:28
 * 素数
 */
public class Test02 {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 101; i < 200; i++) {
            if(isPrime(i)) {
                System.out.print(i + " ");
                count++;
            }
        }

        System.out.println("\n素数的个数为:"+count);
    }
    public static boolean isPrime(int number) {
        for(int j = 2; j <= Math.sqrt(number); j++) {
            if(number%j == 0) {
                return false;
            }
        }
        return true;
    }
}

第三题

打印出所有的“水仙花数”,所谓“水仙花数”是指一个三位数,其各位数字立方和等于该数本身。例如:153是一个“水仙花数”,因为153=13+53+33

/**
 * @author Accepted
 * @create 2021/12/19 12:32
 * 水仙花数
 */
public class Test03 {
    public static void main(String[] args) {
        for(int i = 100; i <= 999; i++) {
            if(isNarc(i)) {
                System.out.println(i);
            }
        }
    }
    public static boolean isNarc(int number) {
        int t = number;
        int sum = 0;
        while(t != 0) {
            sum += Math.pow(t%10, 3);
            t/=10;
        }
        if(number == sum) {
            return true;
        }
        return false;
    }
}

第四题

利用条件运算符的嵌套来完成此题:学习成绩>=90分的同学用A表示,60-89分之间的用B表示,60分以下的用C表示。

import java.util.Scanner;
/**
 * @author Accepted
 * @create 2021/12/19 12:36
 */
public class Test04 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double score = sc.nextDouble();
        if(score >= 90) {
            System.out.println("A");
        } else if (score >= 60) {
            System.out.println("B");
        } else {
            System.out.println("C");
        }
    }
}

第五题

求s=a+aa+aaa+aaaa+aa…a的值,其中a是一个数字。例如2+22+222+2222+22222(此时共有5个数相加),几个数相加有键盘控制。输出结果的形式如:2+22+222=246。

import java.util.Scanner;
/**
 * @author Accepted
 * @create 2021/12/19 12:38
 */
public class Test05 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int m = sc.nextInt();
        int sum = 0, t = a;
        for (int i = 0; i < m; i++) {
            sum += t;
            System.out.print(t);
            if(i != m - 1) {
                System.out.print("+");
            } else {
                System.out.print("=");
            }
            t *= 10;
            t += a;
        }
        System.out.println(sum);
    }
}

第六题

一球从100米高度自由落下,每次落地后反跳回原高度的一半;n次落地经过路线总长度和下次反弹的高度。

import java.util.Scanner;
/**
 * @author Accepted
 * @create 2021/12/19 12:43
 */
public class Test06 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int t = sc.nextInt();
        double n = 100;
        int count = 0;
        double sum = 100;
        for(int i = 0; i < t; i++) {
            count ++;
            System.out.println(count + "次落地路线总长度" + sum + ", 下次反弹高度" + n/2);
            n /= 2;
            sum += n*2;
        }
    }
}

第七题

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

/**
 * @author Accepted
 * @create 2021/12/19 12:50
 * 深度优先搜索做法,也可以朴素做法
 */
public class Test07 {
    static int ans = 0, n = 4;
    final int N = 5;
    int[] st = new int[N];
    boolean[] bool = new boolean[N];
    public static void main(String[] args) {
        Test07 t = new Test07();
        t.dfs(0);
        System.out.println("共有:" + ans + "种可能");
    }
    public void dfs(int u) {
        if(u > n-1) {
            for(int i = 1; i <= n-1; i++) {
                System.out.print(st[i] + " ");
            }
            System.out.println();
            ans++;
            return ;
        }
        for(int i = 1; i <= n; i++) {
            if(!bool[i]) {
                st[u] = i;
                bool[i] = true;
                dfs(u + 1);
                st[u] = 0;
                bool[i] = false;
            }
        }
    }
}

第八题

企业发放的奖金根据利润提成。利润(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,求应发放奖金总数

import java.util.Scanner;
/**
 * @author Accepted
 * @create 2021/12/19 13:04
 */
public class Test08 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        double i = sc.nextDouble();
        double ans = 0;
        if (i <= 10) {
            ans = 0.1 * i;
        } else if (i <= 20) {
            ans = 10 * 0.1 + (i - 10) * 0.075;
        } else if (i <= 40) {
            ans = 10 * 0.1 + 10 * 0.075 + (i - 20) * 0.05;
        } else if (i <= 60) {
            ans = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (i - 40) * 0.03;
        } else if (i <= 100) {
            ans = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (i - 60) * 0.015;
        } else {
            ans = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (i - 100) * 0.01;
        }
        System.out.println(ans);
    }
}

第九题

输入三个整数x、y、z,请把这三个数由小到大输出。

import java.util.Scanner;
/**
 * @author Accepted
 * @create 2021/12/19 13:11
 */
public class Test09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        int y = sc.nextInt();
        int z = sc.nextInt();
        if(x > y) {
            int temp = x;
            x = y;
            y = temp;
        }
        if(x > z) {
            int temp = x;
            x = z;
            z = temp;
        }
        if(y > z) {
            int temp = z;
            z = y;
            y = temp;
        }
        System.out.println(x + " " + y + " " + z);
    }
}

第十题

输入十个整数,请把这十个数由小到大输出。

import java.util.Arrays;
import java.util.Scanner;
/**
 * @author Accepted
 * @create 2021/12/19 13:17
 */
public class Test10 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int[] num = new int[10];
        for (int i = 0; i < num.length; i++) {
            num[i] = sc.nextInt();
        }
        Arrays.sort(num);
        System.out.println(Arrays.toString(num));
    }
}

第十一题

输出九九乘法表。

/**
 * @author Accepted
 * @create 2021/12/19 13:19
 */
public class Test11 {
    public static void main(String[] args) {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                System.out.print(j + "*" + i + "=" + i*j + "\t");
            }
            System.out.println();
        }
    }
}

第十二题

猴子吃桃问题:猴子第一天摘下若干个桃子,当即吃了一半,还不瘾,又多吃了一个第二天早上又将剩下的桃子吃掉一半,又多吃了一个。以后每天早上都吃了前一天剩下的一半零一个。到第10天早上想再吃时,见只剩下一个桃子了。求第一天共摘了多少。

/**
 * @author Accepted
 * @create 2021/12/19 13:20
 */
public class Test12 {
    public static void main(String[] args) {
        int ans = 1;
        for(int i = 1; i < 10; i++) {
            ans = (ans + 1) * 2;
        }
        System.out.println(ans);
    }
}

第十三题

  1. 打印出如下图案。
*****
****
***
**
*
/**
 * @author Accepted
 * @create 2021/12/19 13:23
 */
public class Test13 {
    public static void main(String[] args) {
        for (int i = 0; i < 5; i++) {
            for (int j = 5; j > i; j--) {
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

第十四题

写一个for循环,按从低到高的顺序打印出从1到10的所有数字。

/**
 * @author Accepted
 * @create 2021/12/19 13:24
 */
public class Test14 {
    public static void main(String[] args) {
        for (int i = 1; i <= 10; i++) {
            System.out.println(i);
        }
    }
}

第十五题

有5个人坐在一起,问第五个人多少岁?他说比第4个人大2岁。问第4个人岁数,他说比第3个人大2岁。问第三个人,又说比第2人大两岁。问第2个人,说比第一个人大两岁。最后问第一个人,他说是10岁。请问第五个人多大?

/**
 * @author Accepted
 * @create 2021/12/19 13:27
 */
public class Test15 {
    public static void main(String[] args) {
        int ans = 10;
        for (int i = 1; i < 5; i++) {
            ans += 2;
        }
        System.out.println(ans);
    }
}

第十六题

海滩上有一堆桃子,五只猴子来分。第一只猴子把这堆桃子凭据分为五份,多了一个,这只猴子把多的一个扔入海中,拿走了一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个扔入海中,拿走了一份,第三、第四、第五只猴子都是这样做的,问海滩上原来最少有多少个桃子?

/**
 * @author Accepted
 * @create 2021/12/19 13:30
 */
public class Test16 {
    public static void main(String[] args) {
        int ans = 0;

        while(true) {
            if(finds(ans)) {
                System.out.println(ans);
                break;
            }
            ans++;
        }
    }

    private static boolean finds(int num) {
        //5只猴子
        for(int i = 1; i <= 5 ;i++) {
            if(num > 5 && num % 5 == 1) {
                if(i == 5) {
                    return true;
                } else {
                    num = (num - 1) * 4 / 5;
                }
            } else {
                return false;
            }
        }
        return false;
    }
}

第十七题

求0,1,2,3,4,5,6,7所能组成的8位奇数个数。

/**
 * @author Accepted
 * @create 2021/12/19 13:36
 * 0-7奇数个数
 */
public class Test17 {
    public static void main(String[] args) {
        int count = 0;
        for(int i = 10000000; i <= 77777777; i++) {
            if(i % 2 == 0) {
                continue;
            }
            String str = Integer.toString(i);
            if(fun(str)) {
                count++;
            }
        }
        System.out.println(count);
    }

    public static boolean fun(String str) {
        //此方法来判断是否只包含0-7的数字
        int[] st = new int[10];
        for (int i = 0; i < str.length(); i++) {
            st[str.charAt(i) - '0']++;
        }
        for(int i = 0; i < 8; i++) {
            if(st[i] != 1) {
                return false;
            }
        }
        return true;
    }
}

第十八题

随机生成7个数(1—50的整数),每生成一个数,打印出该相应数量的*。

import java.util.Random;
/**
 * @author Accepted
 * @create 2021/12/19 13:46
 */
public class Test18 {
    public static void main(String[] args) {
        Random random = new Random();
        for(int i = 0; i < 7; i++) {
            int t = random.nextInt(50);
            for (int j = 0; j < t; j++) {
                System.out.print("*");
            }

            System.out.println();
        }
    }
}

友情链接

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小徐学不会.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值