Skip to content

Instantly share code, notes, and snippets.

@shaobin0604
Created April 23, 2012 01:31
Show Gist options
  • Select an option

  • Save shaobin0604/2468101 to your computer and use it in GitHub Desktop.

Select an option

Save shaobin0604/2468101 to your computer and use it in GitHub Desktop.
Equi
package com.tydic;
public class MainCls {
public static void main(String[] args) {
int[] arr = new int[] { 1, 1, 1, 1, 1, 7, 5 };
// tNum1, tNum2 应使用 long 类型,不然数组的加和可能会溢出
int n = arr.length, tNum1 = 0, tNum2 = 0, i, j;
for (i = 0; i < n; i++) {
tNum1 = 0;
tNum2 = 0;
for (j = 0; j <= i; j++) {
tNum1 += arr[j];
}
for (j = n - 1; j >= i; j--) {
tNum2 += arr[j];
}
if (tNum1 == tNum2) {
System.out.println("ϱêΪ£º" + i + "(µÚ" + (i + 1) + ")µÄÔªËØ:"
+ arr[i]);
break;
}
}
// 这里不用判断 tNum1 != tNum2,应直接输出找不到满足条件的位置。
if (tNum1 != tNum2) {
System.out.println("ûÕÒµ½");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment