转:13球称重问题Java实现 收藏

13球称重问题Java实现 收藏[@more@]

/**
* 13球称重问题Java实现
*

Copyright: Copyright (c) 2004


* @author treerot
* @version 1.0
*/
public class ThirteenBall {
private static class Ball {
private int weight;
public int getWeight() {
return weight;
}

public void setWeight(int weight) {
this.weight = weight;
}

Ball(int weight) {
this.weight = weight;
}
}

public static void main(String[] args) throws InterruptedException {
Ball[] balls = new Ball[13];
/*
for (int i = 0; i < 13; i++) {
for (int j = 0; j < 13; j++) {
balls[j] = new Ball(10);
}
//设置一个轻球
balls[i].setWeight(8);
System.out.println(findBall(balls));
//设置一个重球
balls[i].setWeight(12);
System.out.println(findBall(balls));
}
*/
}

static int compare(Ball[] b,int[] b1, int[] b2) {
int left = 0, right = 0;
for (int i = 0; i < b1.length; i++) {
left += b[b1[i]].getWeight();
right += b[b2[i]].getWeight();
}
return left - right;
}

static void print(int flag) {
String s = (flag == 0) ? "无法判断轻重!" :
(flag < 0) ? "目标球比其他轻" : "目标球比其他重";
System.out.println(s);
}

static int findBall(Ball[] b) {
//天平每边放四个球:b[0],b[1],b[2],b[3] VS b[4],b[5],b[6],b[7]
//目标球
int result = 0;

int cmp1 = 0, cmp2 = 0, cmp3 = 0;
cmp1 = compare(b,new int[]{0,1,2,3},new int[]{4,5,6,7});

if (cmp1 == 0) {
//目标球在后面5个中:前面八个球正常,比较 8,9 VS 10,0(也可以是其他0..7)
cmp2 = compare(b,new int[]{8,9},new int[] {10,0});
if (cmp2 == 0) {
//目标球在11,12中:比较 11 VS 0
cmp3 = compare(b,new int[] {11}, new int[] {0});
if (cmp3 == 0) {
//目标球是12
result = 12;
print(0); //这里无法判断轻重
}
else {
//目标球就是11
result = 11;
print(cmp3);
}
}
else if (cmp2 < 0) {
//8,9轻或者10重,比较 8 VS 9
cmp3=compare(b,new int[]{8},new int[]{9});
if(cmp3==0){
//10重
result=10;
print(1);
}
else if(cmp3<0){
//8轻
result=8;
print(-1);
}
else{
//9轻
result=9;
print(-1);
}
}
else{
//8,9重或者10轻
cmp3=compare(b,new int[]{8},new int[]{9});
if(cmp3==0){
//10轻
result=10;
print(-1);
}
else if(cmp3<0){
//9重
result=9;
print(1);
}
else{
//8重
result=8;
print(1);
}

}
}
else if (cmp1 < 0) {
//0,1,2,3轻或者4,5,6,7重 比较:0,1,4 VS 2,3,5
cmp2=compare(b,new int[]{0,1,4},new int[]{2,3,5});
if(cmp2==0){
//6,7重,比较 6 VS 0
cmp3=compare(b,new int[]{6},new int[]{0}); //cmp3不会小于0
if(cmp3==0){
//7重
result=7;
print(1);
}
else{
//6重
result=6;
print(1);
}
}
else if(cmp2<0){
//0,1轻或者5重 比较 0 VS 1
cmp3=compare(b,new int[]{0},new int[]{1});
if(cmp3==0){
//5重
result=5;
print(1);
}
else if(cmp3<0){
//0轻
result=0;
print(-1);
}
else{
//1轻
result=1;
print(-1);
}
}
else{
//2,3轻或者4重 比较 2 VS 3
cmp3=compare(b,new int[]{2},new int[]{3});
if(cmp3==0){
//4重
result=4;
print(1);
}
else if(cmp3<0){
//2轻
result=2;
print(-1);
}
else{
//3轻
result=3;
print(-1);
}
}
}
else {
//和上面对称 0,1,2,3重或者4,5,6,7轻 比较:0,1,4 VS 2,3,5
//实现不愿在写了,这里用递归的话就不止比较三次了,实在是为了偷懒
Ball[] b2=new Ball[13];
System.arraycopy(b,4,b2,0,4);
System.arraycopy(b,0,b2,4,4);
result=findBall(b2)-1;
if(result>=4) result-=4;
else result+=4;
}

return result+1;
}

}


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/treeroot/archive/2004/11/02/164120.aspx

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/220284/viewspace-1028346/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/220284/viewspace-1028346/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值