题目
说明
网上找了找各种答案,感觉怪怪的。大多用了各种字符串转换,自带的sort排序之类的。
初入编程做这个题目的新人应该还不懂这些,刚刚学完基本的+-*/%,所以写了这篇希望能帮助到新人。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| package example;
public class Example4_10 {
public static void main(String[] args) {
int[] array1 = new int[4]; int[] array2 = new int[4];
for (int i = 10; i < 100; i++) { for (int j = i + 1; j < 100; j++) { int resultNum = i * j; if (resultNum < 1000) { continue; } array1[0] = i/10; array1[1] = i%10;
array1[2] = j/10; array1[3] = j%10;
array2[0] = resultNum/1000; array2[1] = resultNum/100%10; array2[2] = resultNum/10%10; array2[3] = resultNum%10;
if (checkZero(array2[2], array2[3]) && sortAndCompareDif(array1) && sortAndCompareDif(array2) && arraysEquals(array1, array2)) { System.out.println(resultNum + "=" + i + "*" + j); } } } }
private static boolean checkZero(int num1, int num2) { return num1 == 0 && num2 == 0 ? false : true; }
private static boolean sortAndCompareDif(int[] nums) { for (int i = 0; i < nums.length - 1; i++) { int count = 0; int tmp =i; for (int j = i + 1; j < nums.length; j++) { if (nums[j] < nums[tmp] ) { tmp = j; } else if (i < 2 && nums[j] == nums[i]) { count += 1; } } if (count < 2) { int tmpNum = nums[tmp]; nums[tmp] = nums[i]; nums[i] = tmpNum; } else { return false; } } return true; }
private static boolean arraysEquals(int[] nums1, int[] nums2) { if (nums1.length != nums2.length) { return false; } for (int index = 0; index < nums1.length; index ++) { if (nums1[index] != nums2[index]) { return false; } } return true; } }
|