首页 > 找次品数学问题

找次品数学问题

有8个球,编号分别是1至8,其中有6个球一样重。另外两个球都轻1克。为了找出这两个轻球,用天平称了三次,结果如下:
第一次: 1+2>3+4
第二次:5+6<7+8
第三次:1+3+5=2+4+8
判断:两个轻球分别是几号?

请问这个题,我想用一小段代码解决?
这个代码怎么写?


没事干 小小尝试了下iOS的代码:

NSMutableArray * numberArr = [[NSMutableArray alloc]initWithObjects:@"0",@"0",@"1",@"1",@"1",@"1",@"1",@"1", nil];

//随机放入好次品
NSMutableArray * arr = [[NSMutableArray alloc]initWithCapacity:8];
for (int i = 0; i < 8; i ++)
{
    int m = arc4random()%numberArr.count;
    [arr addObject:numberArr[m]];
    [numberArr removeObjectAtIndex:m];
}

//1234
if ([arr[0] integerValue] + [arr[1] integerValue] == [arr[2] integerValue] + [arr[3] integerValue])
{
    //1+2 = 3+4
    if ([arr[0] integerValue] + [arr[1] integerValue] == 2)
    {
        //1,2,3,4 都是好的
        NSLog(@"俩次品在5678里面");
        
    }else
    {
        //1 2 有一次品 3 4 有一次品
        if ([arr[0] integerValue] == 0)
        {
            NSLog(@"1是次品");
        }else
        {
            NSLog(@"2是次品");
        }
        
        if ([arr[2] integerValue] == 0)
        {
            NSLog(@"3是次品");
        }else
        {
            NSLog(@"4是次品");
        }
    }
    
}else
{
    if ([arr[0] integerValue] + [arr[1] integerValue] > [arr[2] integerValue] + [arr[3] integerValue])
    {
        //1+2 > 3+4
        if ([arr[2] integerValue] + [arr[3] integerValue] == 1)
        {
            // 3 4 有一个次品
            if ([arr[2] integerValue] == 0)
            {
                NSLog(@"3 为次品");
                
            }else
            {
                NSLog(@"4 为次品");
            }
            
        }else
        {
            //3 4 都为次品
            NSLog(@"3 4 都为次品");
        }
        
    }else
    {
        //1+2 < 3+4
        if ([arr[0] integerValue] + [arr[1] integerValue] == 1)
        {
            // 1 2 有一个次品
            if ([arr[0] integerValue] == 0)
            {
                NSLog(@"1为次品");
            }else
            {
                NSLog(@"2为次品");
            }
            
        }else
        {
            //1 2 都为次品
            NSLog(@"1 2 都为次品");
        }
    }
}

//5678
if ([arr[4] integerValue] + [arr[5] integerValue] == [arr[6] integerValue] + [arr[7] integerValue])
{
    //5+6 = 7+8
    if ([arr[4] integerValue] + [arr[5] integerValue] == 2)
    {
        //5,6,7,8 都是好的
        //NSLog(@"俩次品在1234里面");
        
    }else
    {
        //5 6 有一次品 7 8 有一次品
        if ([arr[4] integerValue] == 0)
        {
            NSLog(@"5是次品");
        }else
        {
            NSLog(@"6是次品");
        }
        
        if ([arr[6] integerValue] == 0)
        {
            NSLog(@"7是次品");
        }else
        {
            NSLog(@"8是次品");
        }
    }
    
}else
{
    if ([arr[4] integerValue] + [arr[5] integerValue] > [arr[6] integerValue] + [arr[7] integerValue])
    {
        //5+6 > 7+8
        if ([arr[6] integerValue] + [arr[7] integerValue] == 1)
        {
            // 7 8 有一个次品
            if ([arr[6] integerValue] == 0)
            {
                NSLog(@"7为次品");
            }else
            {
                NSLog(@"8为次品");
            }
            
        }else
        {
            //7 8 都为次品
            NSLog(@"7 8 都为次品");
        }
        
    }else
    {
        //5+6 < 7+8
        if ([arr[4] integerValue] + [arr[5] integerValue] == 1)
        {
            // 5 6 有一个次品
            if ([arr[4] integerValue] == 0)
            {
                NSLog(@"5为次品");
                
            }else
            {
                NSLog(@"6为次品");
            }
            
        }else
        {
            //5 6 都为次品
            NSLog(@"5 6 都为次品");
        }
    }
}

NSLog(@"好次品的位置排序:%@",arr);

下面是几次输出的结果....
{
  本来想用1+2+3+4?=5+6+7+8的方法来一一判断的,突然发现编程里面是不可能碰到这种事情的,也就是说不合逻辑.然后后面的判断取了巧,根本没难度.但是,这里出现了一种最基本的算法,折中查询.也就是说想到知道某个数值在数组第几位,最快的方法就是折中查询.前提是数组内的值是有序的...
  如果有序,折半查找的话,代码就更简单了吧!
}




稍微动下脑子就会知道答案应该是4号和5号咯。不过代码这东西有点死,并没有人脑来的那么智能。那咋办呢?最简单的方法是列出所有的可能情况然后对每种情况按照条件过滤。给个 JS 的示例代码,打开浏览器控制台复制运行可查看结果:

// 先列出所有的可能情况,假设正常的球是 1 克,那么请求则为 0 克
// 从 8 个球里面挑 2 个轻的,应该会有 8 * 7 / 2 = 28 种情况
var count = 8, normal = 1, light = 0;
var arr = [];
for(var i=0;i<8;i++) {
  for(var j=i+1;j<8;j++) {
    var a = (new Array(count+1)).join().split(",").map(function(){return normal});
    a[i] = a[j] = light;
    arr.push(a);
  }
}

// 对每个情况进行条件判断是否符合条件
var res = arr.filter(function(a) {
  return (a[0]+a[1]>a[2]+a[3]) && 
         (a[4]+a[5]<a[6]+a[7]) && 
         (a[0]+a[2]+a[4]===a[1]+a[3]+a[7])
});

// 输出结果
console.log("符合条件的情况是:");
console.table(res);

res = res.map(function(a){ 
    var k = [];
    a.forEach(function(v,i){ 
        if(v===light) k.push(i+1);
    });
    return k;
});
console.log("符合条件的轻球号为: ");
console.table(res);
【热门文章】
【热门文章】