实例 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 - 令
,
,
,
和
至少一个是偶数
- 那么
%20%2F%202#card=math&code=m%20%3D%20%28i%20%2B%20j%29%20%2F%202&id=WMMnf),
%20%2F%202#card=math&code=n%20%3D%20%28i%20-%20j%29%20%2F%202&id=xAghe),
和
要么都是偶数,要么都是奇数
- 从上面两部推导可知,
与
均是不小于 2 的偶数
- 由于
,$ j>=2$,则
。
- 接下来将
的所有数字循环计算即可
实现
/*** 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、num2if (num1 > num2) {int tmp = num1;num1 = num2;num2 = tmp;}// 交换 num1、num3if (num1 > num3) {int tmp = num1;num1 = num3;num3 = tmp;}// 交换 num2、num3if (num2 > num3) {int tmp = num2;num2 = num3;num3 = tmp;}System.out.format("三个数从小到大的顺序:%d < %d < %d", num1, num2, num3);}}
结果

