62,614
社区成员
发帖
与我相关
我的任务
分享
/**
* compute the result: x1y1+x2y2+x3y3+x4y4+x5y5+x6y6
* x1=10^20,x2=1223,x3=10^18,x4=10^15,x5=3,x6=-10^12
* y1=10^20,y2=2,y3=-10^22,y4=10^13,y5=2111,y6=10^16
* <p>
* note:skip all pre check
*/
public class ApplicationSolution {
public static void main(String[] args) {
int[][][] arrays = init();
int[][] bigs = arrays[0];
int[][] tinys = arrays[1];
sortByPower(bigs);
if (bigs.length % 2 != 0) {
throw new IllegalStateException("The big array is not illegal,it must be even elements!");
}
int[] powers = sortByPower(bigs);
if (!pair(powers)) {
outputResult("NaN");
System.exit(0);
}
int result = 0;
for (int[] tiny : tinys) {
result += tiny[0] * tiny[1];
}
outputResult(result);
}
/**
* 根据幂排序,如果为负数则*-1
* @param bigs 原始大数元素数组
* @return 所有大数的幂
*/
private static int[] sortByPower(int[][] bigs) {
int[][] nums = bigs;
int[] powers = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
powers[i] = getCurrentPower(nums[i]);
}
Arrays.sort(powers);
return powers;
}
/**
* 检查是否所有幂指数都可以被抵消
* @param powers 幂指数
* @return 抵消结果
*/
private static boolean pair(int[] powers) {
int[] nums = powers;
int leftIndex = 0, rightIndex = nums.length - 1;
int left = nums[leftIndex];
int right = nums[rightIndex];
while (true) {
if (left + right != 0) {
return false;
}
leftIndex++;
rightIndex--;
if (leftIndex < rightIndex) {
break;
}
}
return true;
}
/**
* 获取当前数据的幂指数
* @param nums 原始数据
* @return 幂指数
*/
private static int getCurrentPower(int[] nums) {
int power = 0;
for (int j = 1; j < nums.length; j += 2) {
power += nums[j];
}
if (nums[0] < 0) {
power *= -1;
}
return power;
}
/**
* 使用输入数据初始化
* 其中分为两个部分,一个是大数二维数组,一个是基本数数组
* @return array[0]为大数元素,array[1]为基本数组
*/
private static int[][][] init() {
int x11 = 10, x12 = 20, y11 = 10, y12 = 20;
int[] xy1 = new int[]{x11, x12, y11, y12};
int x2 = 1223, y2 = 2;
int[] xy2 = new int[]{x2, y2};
int x31 = -10, x32 = 18, y31 = 10, y32 = 22;
int[] xy3 = new int[]{x31, x32, y31, y32};
int x41 = 10, x42 = 15, y41 = 10, y42 = 13;
int[] xy4 = new int[]{x41, x42, y41, y42};
int x5 = 3, y5 = 2111;
int[] xy5 = new int[]{x5, y5};
int x61 = -10, x62 = 12, y61 = 10, y62 = 16;
int[] xy6 = new int[]{x61, x62, y61, y62};
return new int[][][]{new int[][]{xy1, xy3, xy4, xy6}, new int[][]{xy2, xy5}};
}
/**
* 输出结果
* @param result 结果值
*/
private static void outputResult(Object result) {
System.out.println("The result is:" + result);
}
}
public class Test2 {
public static void main(String[] args) {
BigDecimal b1 = new BigDecimal(10).pow(20).multiply(new BigDecimal(10).pow(20));
BigDecimal b2 = new BigDecimal(1223).multiply(new BigDecimal(2));
BigDecimal b3 = new BigDecimal(10).pow(18).multiply(new BigDecimal(-10).pow(22));
BigDecimal b4 = new BigDecimal(10).pow(15).multiply(new BigDecimal(10).pow(13));
BigDecimal b5 = new BigDecimal(3).multiply(new BigDecimal(2111));
BigDecimal b6 = new BigDecimal(-10).pow(12).multiply(new BigDecimal(10).pow(16));
BigDecimal result1 = new BigDecimal(0);
BigDecimal result2 = new BigDecimal(0);
result1 = b1.add(b2).add(b3).add(b4).add(b5).add(b6);
result2 = b1.add(b2).subtract(b3).add(b4).add(b5).subtract(b6);
System.out.println(result1);
System.out.println(result2);
}
}
public class Sample {
public static void main(String[] args) throws Exception {
try {
int x1 = power(10, 20);
int x2 = 1223;
int x3 = power(10, 18);
int x4 = power(10, 15);
int x5 = 3;
int x6 = -power(10, 12);
int y1 = power(10, 20);
int y2 = 2;
int y3 = -power(10, 22);
int y4 = power(10, 13);
int y5 = 2111;
int y6 = power(10, 16);
System.out.println(x1*y1+x2*y2+x3*y3+x4*y4+x5*y5+x6*y6);
} catch (Exception e) {
e.printStackTrace();
}
}
public static int power(int base, int times) {
if (times == 0) return 1;
else if (times == 1) return base;
for (int i=1; i<times; i++) {
base *= base;
}
return base;
}
}