package cn;
public class BalancePiont {
/**
* 平衡点:比如int[] numbers = {1,3,5,7,8,25,4,20};
* 25前面的总和为24,25后面的总和也是24,25这个点就是平衡点;假如一个数组中的元素,其前面的部分等于后面的部分,那么这个点的位序就是平衡点
* 要求:返回任何一个平衡点
*/
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int number[] = { 1, 3, 5, 7, 8, 25, 4, 20 };
int i = 1;
int size = number.length;
int balancePiont = 0;
while (i < size) {
int bPAfterSun = 0;
int bPBeforeSum = 0;
balancePiont = number[i];
for (int j = 0; j < i; j++) {
bPBeforeSum += number[j];
}
for (int j = i + 1; j < size; j++) {
bPAfterSun += number[j];
if (bPAfterSun > bPBeforeSum)
break;
}
System.out.println("bPBeforeSum:" + bPBeforeSum + ",bPAfterSun:"
+ bPAfterSun);
if (bPBeforeSum == bPAfterSun) {
System.out.println("平衡点:" + balancePiont);
break;
}
i++;
System.out.println(i);
if (i == size && bPBeforeSum != bPAfterSun) {
System.out.println("没有找到平衡点");
}
}
}
}