1.1题目要求
对于所给两组数{2,3,5,8}和{1,4,6,7},从一组数据中分别取数与另一组中的数配对,每个配对中的数字组成两位数,求平方和。例如
87^2 + 56^2 + 34^2 + 21^2 = 12302
78^2 + 65^2 + 43^2 + 12^2 = 12302
1.2解决方法
将一组数据存入一个数组内,对于一组数据进行排列组合,改变数组下标对应的数字,不断与另一组数据项匹配,完成每一种可能的匹配。然后按照题目要求求出平方和,判断是否符合
1.3程序
1.3.1程序
package CaseOne;
public class First {
public static int count =0;
public static int arr1[] = {2,3,5,8};
public static int arr2[] = {1,4,6,7};
public static void main(String[] arg) {
fullSort(arr2, 0, 3);
System.out.println(count);
}
public static void fullSort(int[] arr, int start, int end) {
if(start == end) {
check();
return;
}
for(int i = start; i <= end; i++) {
change(arr, start, i);
fullSort(arr,start + 1,end);
change(arr, i, start);
}
}
//全排列
public static void change(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
//交换元素位置
public static void check() {
int a = (int) (Math.pow(arr1[0]*10+arr2[0],2) + Math.pow(arr1[1]*10+arr2[1],2) + Math.pow(arr1[2]*10+arr2[2],2) + Math.pow(arr1[3]*10+arr2[3],2));
int b = (int) (Math.pow(arr2[0]*10+arr1[0],2) + Math.pow(arr2[1]*10+arr1[1],2) + Math.pow(arr2[2]*10+arr1[2],2) + Math.pow(arr2[3]*10+arr1[3],2));
//计算个位十位交换前后的平方和
if(a == b) {
count++;
}
}
}