java经典40道编程题(11~20)

    xiaoxiao2025-03-03  33

    第十一题:

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

    参考答案

    public class Test11 { public static void main(String[] args) { int count = 0; for (int i = 1; i < 5; i++) {// 最外层循环,控制百位数; for (int j = 1; j < 5; j++) {// 第二层循环控制十位数; for (int z = 1; z < 5; z++) {// 第三层循环控制个位数; if (i != j && i != z && j != z)// 如果三个位上的值互不相等,执行计数操作; { count++; System.out.println(i * 100 + j * 10 + z); } } } } System.out.println("共有" + count + "个这样的数!"); } }
    第十二题:

    企业发放的奖金根据利润提成。利润(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%提成,从键盘输入当月利润,求应发放奖金总数?

    参考答案

    public class Test12 { public static void main(String[] args) { float I = 0; float Monney = 0; System.out.println("请输入企业的利润: (单位:万元)"); Scanner sc = new Scanner(System.in); I = sc.nextFloat(); //判断企业的利润,进行相应等级的奖金计算 if (I <= 10) { Monney = (float) (I * 0.1); } if (10 < I && I <= 20) { Monney = (float) (I * 0.1) + (float) (I * 0.075); } if (20 < I && I <= 40) { Monney = (float) (I * 0.1) + (float) (I * 0.075) + (float) (I * 0.05); } if (40 < I && I <= 60) { Monney = (float) (I * 0.1) + (float) (I * 0.075) + (float) (I * 0.05) + (float) (I * 0.03); } if (60 < I && I <= 100) { Monney = (float) (I * 0.1) + (float) (I * 0.075) + (float) (I * 0.05) + (float) (I * 0.03); } if (I > 100) { Monney = (float) (I * 0.1) + (float) (I * 0.075) + (float) (I * 0.05) + (float) (I * 0.03) + (float) (I * 0.01); } System.out.println("经计算,您的奖金总数为:" + Monney); } }
    第十三题:

    一个整数,它加上 100 后是一个完全平方数,再加上 168 又是一个完全平方数,请问该数是多少?

    参考答案

    public class Test13 { public static void main(String[] args) { int i = 0; while (true) { if (Math.sqrt(i + 100) % 1 == 0) { if (Math.sqrt(i + 100 + 168) % 1 == 0) { System.out.println(i + "是完全平方数"); } } i++; if (i > 10000) { break; } } } }
    第十四题:

    输入某年某月某日,判断这一天是这一年的第几天?

    参考答案

    public class Test14 { public static void main(String[] args) { int day; int month; int year; int monthArray1[] = { 0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 闰年各月份天数 int monthArray2[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; // 其他年月份天数 System.out.println("请输入年份:"); Scanner sc1 = new Scanner(System.in); year = sc1.nextInt(); System.out.println("请输入月份:"); Scanner sc2 = new Scanner(System.in); month = sc2.nextInt(); System.out.println("请输入几号:"); Scanner sc3 = new Scanner(System.in); day = sc3.nextInt(); if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {// 判断是否是闰年 for (int i = 1; i <= month; i++) { day = monthArray1[i] + day;// 把月份之前的所有天数加起来 } System.out.println("这一天是这一年的第:" + day + "天"); } else { for (int i = 1; i <= month; i++) { day = monthArray2[i] + day;// 把月份之前的所有天数加起来 } System.out.println("这一天是这一年的第:" + day + "天"); } } }
    第十五题:

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

    参考答案

    import java.util.Arrays; import java.util.Scanner; public class Test15 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入x:"); int x = sc.nextInt(); System.out.println("请输入y:"); int y = sc.nextInt(); System.out.println("请输入z:"); int z = sc.nextInt(); long startTime = System.currentTimeMillis(); int intArray[] = { x, y, z };// 创建一个整形数组保存输入的数据 Arrays.sort(intArray);// 将数组按升序排序 System.out.println("从小到大的排序为:"); for (int i = 0; i < intArray.length; i++) { System.out.println(intArray[i]); } long endTime = System.currentTimeMillis(); System.out.println("花费的时间为:" + (endTime - startTime)); } }
    第十六题:

    输出 9*9 口诀。

    参考答案

    public class Test16 { public static void main(String[] args) { int i, j; for (i = 1; i < 10; i++) {// 控制列 for (j = 1; j <= i; j++) {// 控制行 System.out.print(j + "*" + i + "=" + j * i + "\t"); // 输出乘法表 } System.out.println();// 换行 } } }
    第十七题:

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

    参考答案

    public class Test17 { public static void main(String[] args) { int number=1; for(int i=9;i>0;i--) { number=(number+1)*2; } System.out.println("第一天摘了"+number+"个桃子"); } }
    第十八题:

    两个乒乓球队进行比赛,各出三人。甲队为 a,b,c 三人,乙队为 x,y,z 三人。已抽签决定比赛名单。有人向队员打听比赛的名单。a 说他不和 x 比,c 说他不和 x,z 比,请编程序找出三队赛手的名单。

    参考答案

    public class Test18 { static char[] m = { 'a', 'b', 'c' };// 把要处理的字符放进字符数组中便于处理; static char[] n = { 'x', 'y', 'z' }; public static void main(String[] args) { for (int i = 0; i < m.length; i++) {// 外层循环遍历甲队队员, for (int j = 0; j < n.length; j++) {// 内层循环遍历乙队队员, if (m[i] == 'a' && n[j] == 'x') continue; // 根据题意知道c对战y,a不可能对战y; else if (m[i] == 'a' && n[j] == 'y') continue; // 根据题意; else if ((m[i] == 'c' && n[j] == 'x') || (m[i] == 'c' && n[j] == 'z')) continue; // 推测出b不可能对战y和z; else if ((m[i] == 'b' && n[j] == 'y') || (m[i] == 'b' && n[j] == 'z')) continue; else System.out.println(m[i] + "对战" + n[j]); } } } }
    第十九题:

    用*打印出图案(菱形)

    参考答案

    public class Test19 { public static Scanner input = new Scanner(System.in); public static void main(String[] args) { System.out.println("请输入你要显示的总行数(奇数):"); int num = input.nextInt(); for (int i = 1; i <= (num+1) / 2; i++) {//此循环是控制上层的三角的,包括最中间的一行; for (int j = 0; j < (num+1) / 2 -i ; j++) {//控制每一行的空格数 System.out.print(" "); } for (int j = 0; j < 2*i - 1; j++) {//控制每一行显示的*符号数 System.out.print("*"); } System.out.println();//换行 } for (int i = 1; i <= (num -1 ) / 2; i++) {//此循环是控制下层的三角的 for (int j = 0; j < i ; j++) {//控制每一行的空格数 System.out.print(" "); } for (int j = 0; j < num - 2*i; j++) {//控制每一行显示的*符号数 System.out.print("*"); } System.out.println();//换行 } } }
    第二十题:

    有一分数序列:2/1,3/2,5/3,8/5,13/8,21/13…求出这个数列的前 20 项之和

    *参考答案

    import java.util.Scanner; public class CorrectAnswer { public static void main(String[] args) { double sum = 0; double n1 = 1, n2 = 2, sn = 0;// 经分析,分子分母都为斐波那契数列 for (int i = 1; i <= 20; i++) {// 数列前20项,循环20次 sum += n2 / n1; sn = n1 + n2; n1 = n2; n2 = sn; } System.out.println("这个数列的前20项之和为:" + sum); } }

    干货满满,给个赞呗!

    最新回复(0)