惨不忍睹的代码:package com.justsy.army.mgt.bwlist.controller;
import java.util.Scanner;
import org.apache.commons.lang.ArrayUtils;
public class test {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("输入若干个数字(逗号分隔):");
String nextLine = scanner.nextLine();
String[] split = nextLine.split(",");
int[] students = convert2IntArray(split);
//消除奇数
for( int i = 0;i<students.length;i++ ) {
if( students[i] % 2 != 0 ) {
students[i] += 1;
}
}
//开始每轮游戏
while(!isEveryoneEqual(students)) {
//给左边
int giveNext = students[students.length-1] / 2;
for( int i = 0;i<students.length;i++ ) {
int toAdd = giveNext;
giveNext = students[i] / 2;
students[i] = giveNext + toAdd;
}
//消除奇数
for( int i = 0;i<students.length;i++ ) {
if( students[i] % 2 != 0 ) {
students[i] += 1;
}
}
//每轮输出
System.out.println(ArrayUtils.toString(students));
}
System.out.printf("每个人都相等了:%d",students[0]);
}
public static boolean isEveryoneEqual(int[] students) {
int temp = students[0];
for( int i = 1;i<students.length;i++ ) {
if( students[i] != temp )return false;
}
return true;
}
public static int[] convert2IntArray(String[] split) {
int[] ret = new int[split.length];
for(int i = 0;i<split.length;i++) {
ret[i] = Integer.parseInt(split[i]);
}
return ret;
}
}
|