Created
April 23, 2012 01:31
-
-
Save shaobin0604/2468101 to your computer and use it in GitHub Desktop.
Equi
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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