有1、2、3、4个数字,能组成多少个互不相同且无重复数字的三位数?都是多少?
填在各位上的数字都可以是 1、2、3、4,但是只要满足各位上的数不相同,就可以当成一个组成的数。
1/**
2 * @ClassName : One
3 * @Author : cunyu1943
4 * @Date : 2020/6/18 11:17
5 * @Version : 1.0
6 * @Description : 实例 1
7 **/
8
9public class One {
10 public static void main(String[] args) throws Exception {
11 // 用于计数
12 int count = 0;
13
14 System.out.println("组成的数:");
15 // 设置三重循环,对每位上的数进行赋值
16 for (int i = 1; i < 5; i++) {
17 for (int j = 1; j < 5; j++) {
18 for (int k = 1; k < 5; k++) {
19 // 将不满足条件的排列去掉
20 if (i != j && i != k && k != j) {
21 // 满足条件则计数 + 1
22 count = count + 1;
23 System.out.print("" + (i * 100 + j * 10 + k)+"\t");
24 // 每输出 5 个则换行输出
25 if (count % 5 ==0){
26 System.out.println();
27 }
28 }
29 }
30
31 }
32 }
33
34 System.out.println("\n满足条件的数有:" + count + " 个");
35 }
36}
1组成的数:
2123 124 132 134 142
3143 213 214 231 234
4241 243 312 314 321
5324 341 342 412 413
6421 423 431 432
7满足条件的数有:24 个
企业发放的奖金根据利润提成。
从键盘输入当月利润,求应发放奖金总数?
可以将利润利用各个阈值进行分界,先将各个临界点的奖金算出来,然后再对各区间进行处理
1import java.util.Scanner;
2
3/**
4 * @ClassName : Two
5 * @Author : cunyu1943
6 * @Date : 2020/6/18 11:44
7 * @Version : 1.0
8 * @Description : 实例 2
9 **/
10
11public class Two {
12 public static void main(String[] args) throws Exception {
13 // 最终奖金
14 double bonus = 0.0;
15 System.out.println("输入利润:");
16 Scanner input = new Scanner(System.in);
17 // 利润
18 long profit = input.nextLong();
19 // 将各个临界点的奖金算出来
20 double baseBonus = 100000 * 0.1;
21 double bonus1 = baseBonus + 100000 * 0.075;
22 double bonus2 = bonus1 + 200000 * 0.05;
23 double bonus3 = bonus2 + 200000 * 0.03;
24 double bonus4 = bonus3 + 400000 * 0.015;
25
26
27 if (profit <= 100000) {
28 // 利润低于或等于10万元时
29 bonus = profit * 0.1;
30 } else if (profit <= 200000) {
31 // 利润高于10万元,低于20万元时
32 bonus = baseBonus + (profit - 100000) * 0.075;
33 } else if (profit <= 400000) {
34 // 20万到40万之间时
35 bonus = bonus1 + 0.05 * (profit - 200000);
36 } else if (profit <= 600000) {
37 // 40万到60万时
38 bonus = bonus2 + (profit - 400000) * 0.03;
39 } else if (profit <= 1000000) {
40 // 60万到100万之间时
41 bonus = bonus3 + (profit - 600000) * 0.015;
42 } else if (profit > 1000000) {
43 // 高于100万元时
44 bonus = bonus4 + (profit - 1000000) * 0.01;
45 }
46
47 System.out.println("当月奖金为:" + bonus);
48 }
49}
1输入利润:
2120000
3当月奖金为:11500.0
一个整数,它加上100后是一个完全平方数,再加上168又是一个完全平方数,请问该数是多少?
完全平方数:若一个数能表示成某个正数的平方的形式,则说该数是一个完全平方数
,(n - m)(n + m) = 168
,则说明
或者
其中必有一个偶数
,则
,说明 均为偶数,或者均为奇数
1/**
2 * @ClassName : Three
3 * @Author : cunyu1943
4 * @Date : 2020/6/18 13:40
5 * @Version : 1.0
6 * @Description : 实例 3
7 **/
8
9public class Three {
10 public static void main(String[] args) throws Exception {
11 // 最终结果
12 int result = 0;
13
14 System.out.println("最终结果可能的值:");
15 for (int i = 2; i < (168 / 2 + 1); i++) {
16 // i,j 为偶数,i * j = 168
17 if (168 % i == 0) {
18 int j = 168 / i;
19 // i = n + m,j = n - m,且 i, j 均为偶数,偶数之和/差仍为偶数
20 if (i > j && (i + j) % 2 == 0 && (i - j) % 2 == 0) {
21 int n = (i + j) / 2;
22 int m = (i - j) / 2;
23 result = m * m - 100;
24 System.out.println("" + result);
25 }
26 }
27 }
28
29 }
30}
1最终结果可能的值:
2-99
321
4261
51581
输入某年某月某日,判断这一天是这一年的第几天?
同实例 2 类似,先以平年为基准将各个临界点的值事先算出来,然后再做后续处理,针对特殊情况闰年,则当月份大于 2 时,在平年的基础上加 1 即可
1import java.util.Scanner;
2
3/**
4 * @ClassName : Four
5 * @Author : cunyu1943
6 * @Date : 2020/6/18 14:29
7 * @Version : 1.0
8 * @Description : 实例 4
9 **/
10
11public class Four {
12 public static void main(String[] args) throws Exception {
13 Scanner input = new Scanner(System.in);
14
15 System.out.println("分别输入年月日,输入一个值则回车一次");
16 int year = input.nextInt();
17 int month = input.nextInt();
18 int day = input.nextInt();
19
20 // 某年的第几天
21 int sum = 0;
22
23 // 先以平年为基准
24 switch (month) {
25 case 1:
26 sum = 0;
27 break;
28 case 2:
29 sum = 31;
30 break;
31 case 3:
32 sum = 59;
33 break;
34 case 4:
35 sum = 90;
36 break;
37 case 5:
38 sum = 120;
39 break;
40 case 6:
41 sum = 151;
42 break;
43 case 7:
44 sum = 181;
45 break;
46 case 8:
47 sum = 212;
48 break;
49 case 9:
50 sum = 243;
51 break;
52 case 10:
53 sum = 273;
54 break;
55 case 11:
56 sum = 304;
57 break;
58 case 12:
59 sum = 334;
60 break;
61 default:
62 break;
63 }
64
65 // 判别闰平年
66 boolean leap = false;
67
68 if (year % 400 == 0 || (year % 4 == 0 & year % 100 != 0)) {
69 leap = true;
70 }
71 // 某月之前的天数加上当月的天数
72 sum = sum + day;
73
74 // 是闰年且月份大于 2 则在平年的基础上 + 1
75 if (leap == true && month > 2) {
76 sum = sum + 1;
77 }
78
79 System.out.println("输入的日期是该年的第 " + sum + " 天!");
80 }
81}
1分别输入年月日,输入一个值则回车一次
22020
36
418
5输入的日期是该年的第 170 天!
输入三个整数x,y,z,请把这三个数由小到大输出。
1import java.util.Arrays;
2import java.util.Scanner;
3
4/**
5 * @ClassName : Five
6 * @Author : cunyu1943
7 * @Date : 2020/6/18 14:46
8 * @Version : 1.0
9 * @Description : 实例 5
10 **/
11
12public class Five {
13 public static void main(String[] args) throws Exception {
14 Scanner input = new Scanner(System.in);
15
16 // 用于存放输入的 3 个数
17 int[] array = new int[3];
18 System.out.println("输入三个数,每输入一个回车一次:");
19 for (int i = 0; i < 3; i++) {
20 array[i] = input.nextInt();
21 }
22
23 // 对数组排序输出
24 for (int i = 0; i < array.length - 1; i++) {
25 for (int j = 0; j < array.length - 1 - i; j++) {
26 // 前后比较,将大的放在后边
27 if (array[j] > array[j + 1]) {
28 int tmp = array[j];
29 array[j] = array[j + 1];
30 array[j + 1] = tmp;
31 }
32 }
33 }
34
35 // 打印排序后的数组
36 System.out.println("三个数从小到大:");
37 for (int item : array
38 ) {
39 System.out.print(item + "\t");
40 }
41 }
42}
1输入三个数,每输入一个回车一次:
243
354
42
5三个数从小到大:
62 43 54