本文已同步至:村雨遥

实例 11

题目

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

剖析

间接三重循环,而后加一个判断语句,让三位数的各位上的数都不雷同即可!

实现

/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @date : 2021/6/2 16:46 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example11 * @description : */public class Example11 {    public static void main(String[] args) {        int count = 0;        System.out.println("组成的三位数是:");        for (int i = 1; i < 5; i++) {            for (int j = 1; j < 5; j++) {                if (i != j) {                    for (int k = 1; k < 5; k++) {                        if (i != k && j != k) {                            count++;                            System.out.print((i * 100 + j * 10 + k) + "\t");                            // 每打印 5 个就换行                            if (count % 5 == 0) {                                System.out.println();                            }                        }                    }                }            }        }        System.out.println("\n共有 " + count + " 个不反复的三位数");    }}

后果

实例 12

题目

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

剖析

分段计算利润即可;

实现

import java.util.Scanner;/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @date : 2021/6/2 17:14 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example12 * @description : */public class Example12 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        System.out.println("输出当月利润(万)");        int profit = scanner.nextInt();        double bonus = 0;        if (profit <= 10) {            bonus = profit * 0.1;        } else if (10 < profit && profit <= 20) {            bonus = 10 * 0.1 + (profit - 10) * 0.075;        } else if (20 < profit && profit <= 40) {            bonus = 10 * 0.1 + 10 * 0.075 + (profit - 20) * 0.05;        } else if (40 < profit && profit <= 60) {            bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + (profit - 40) * 0.03;        } else if (60 < profit && profit <= 100) {            bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + (profit - 60) * 0.015;        } else if (profit > 100) {            bonus = 10 * 0.1 + 10 * 0.075 + 20 * 0.05 + 20 * 0.03 + 40 * 0.015 + (profit - 100) * 0.01;        }        System.out.println("利润为 " + profit + " 时的奖金为:" + bonus + " 万");    }}

后果

实例 13

题目

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

剖析

假如这个数是 num,那么就有:

  • num + 100 = n * n, num + 100 + 168 = m * m
  • m * m - n * n = (m + n)(m - n) = 168
  • 令 $m + n = i$,$m - n = j$,$i * j =168$,$i$ 和 $j$ 至多一个是偶数
  • 那么 $m = (i + j) / 2$, $n = (i - j) / 2$,$i$ 和 $j$ 要么都是偶数,要么都是奇数
  • 从下面两部推导可知,$i$ 与 $j$ 均是不小于 2 的偶数
  • 因为 $i * j = 168$,$ j>=2$,则 $1 < i <= 168 / 2$。
  • 接下来将 $i$ 的所有数字循环计算即可

实现

/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @date : 2021/6/3 9:01 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example13 * @description : */public class Example13 {    public static void main(String[] args) {        int m = 0;        int n = 0;        int num = 0;        System.out.println("该数可能是:");        for (int i = 1; i <= (168 / 2); i++) {            if (168 % i == 0) {                int j = 168 / i;                if (i > j && (i + j) % 2 == 0 && (i - j) % 2 == 0) {                    m = (i + j) / 2;                    n = (i - j) / 2;                    num = n * n - 100;                    System.out.print(num + "\t");                }            }        }    }}

后果

实例 14

题目

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

剖析

别离输出年月日,而后优先判断是否为平年,而后依据是否平年给出 2 月的天数,最初就是 switch 匹配月份,把天数相加即可。

实现

import java.util.GregorianCalendar;import java.util.Scanner;/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @date : 2021/6/3 9:39 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example14 * @description : */public class Example14 {    public static void main(String[] args) {        Scanner scanner = new Scanner(System.in);        int year = 0;        int month = 0;        int day = 0;        System.out.println("输出年:");        year = scanner.nextInt();        System.out.println("输出月:");        month = scanner.nextInt();        System.out.println("输出日:");        day = scanner.nextInt();        // 判断是否是平年        // GregorianCalendar:判断年份是否是平年的办法        GregorianCalendar gre = new GregorianCalendar();        // 返回true:是平年,false:不是平年        boolean isLeapYear = gre.isLeapYear(year);//        2 月份的天数        int feb = isLeapYear ? 29 : 28;        int dayOfYear = 0;        switch (month) {            case 1:                dayOfYear = day;                break;            case 2:                dayOfYear = 31 + day;                break;            case 3:                dayOfYear = 31 + feb + day;                break;            case 4:                dayOfYear = 31 + feb + 31 + day;                break;            case 5:                dayOfYear = 31 + feb + 31 + 30 + day;                break;            case 6:                dayOfYear = 31 + feb + 31 + 30 + 31 + day;                break;            case 7:                dayOfYear = 31 + feb + 31 + 30 + 31 + 30 + day;                break;            case 8:                dayOfYear = 31 + feb + 31 + 30 + 31 + 30 + 31 + day;                break;            case 9:                dayOfYear = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + day;                break;            case 10:                dayOfYear = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;                break;            case 11:                dayOfYear = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;                break;            case 12:                dayOfYear = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;                break;            default:                System.out.println("月份输出谬误");                break;        }        System.out.println("这一天是这一年的第 " + dayOfYear + " 天!");    }}

后果

实例 15

题目

输出三个整数 num1、num2、num3,请把这三个数从小到大输入。

剖析

别离输出三个数,而后两两之间比拟并替换,小的在前,大的在后,最初从小到大输入三个数即可;

实现

import java.util.Scanner;/** * Created with IntelliJ IDEA. * * @author : cunyu * @version : 1.0 * @email : 747731461@qq.com * @公众号 : 村雨遥 * @website : https://cunyu1943.github.io * @date : 2021/6/3 10:08 * @project : Java 编程实例 * @package : PACKAGE_NAME * @className : Example15 * @description : */public class Example15 {    public static void main(String[] args) {        int num1 = 0;        int num2 = 0;        int num3 = 0;        Scanner scanner = new Scanner(System.in);        System.out.println("输出第 1 个数");        num1 = scanner.nextInt();        System.out.println("输出第 2 个数");        num2 = scanner.nextInt();        System.out.println("输出第 3 个数");        num3 = scanner.nextInt();//        替换 num1、num2        if (num1 > num2) {            int tmp = num1;            num1 = num2;            num2 = tmp;        }//        替换 num1、num3        if (num1 > num3) {            int tmp = num1;            num1 = num3;            num3 = tmp;        }//        替换 num2、num3        if (num2 > num3) {            int tmp = num2;            num2 = num3;            num3 = tmp;        }        System.out.format("三个数从小到大的程序:%d < %d < %d", num1, num2, num3);    }}

后果