平衡点

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("没有找到平衡点");
   }

  }

 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值