CPU消耗 < 1000ms
import java.util.Scanner;
public class FenTangGuo {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int tangGuo = 0;
int temp;
int array[] = new int[n];
for (int i = 0; i < n; i++) {
array[i] = sc.nextInt();
}
while (true) {
int x=0; //x用于记录数组中相同元素的个数
for (int i = 0; i < n; i++) {
if (array[i] % 2 != 0) {
array[i]++;
tangGuo++;
}
}
for (int i = 0; i < n; i++) {
array[i] = array[i] / 2;
}
temp = array[0]; //将第一个元素的一半赋值给临时变量
for (int i = 0; i < n - 1; i++) {
array[i] = array[i] + array[i + 1];
}
array[n - 1] = array[n - 1] + temp;
for (int i = 0; i < n - 1; i++) {
if (array[i] == array[i + 1])
++x;
}
if (x == n-1) {
System.out.print(tangGuo);
break;
}
}
}
}
欢迎斧正。原文:http://blog.csdn.net/xurunhong/article/details/23305815