[spoiler]
// file type: console application
// description : of twelve identical looking objects
// identify the one object which is heavier or lighter
// in a maximum of three balance scale observations
#include <tchar.h>
#include <iostream>
int obj[] = { 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0 };
struct ScaleResult
{
bool balanced;
bool groupA_isHeavier;
bool groupB_isHeavier;
} scaleResult;
ScaleResult testWeight(int groupA, int groupB)
{
ScaleResult result = { false, false, false };
if (groupA == groupB)
result.balanced = true;
else if (groupA > groupB)
result.groupA_isHeavier = true;
else
result.groupB_isHeavier = true;
return result;
}
void printResult(int index, bool isHeavier)
{
std::cout << "object weights = { ";
for (int i = 0; i < 12; ++i) { std::cout << obj[i] << " "; }
std::cout << "}\n";
std::cout << "object number " << index + 1 << " is ";
(isHeavier) ? std::cout << "heavier\n" : std::cout << "lighter\n";
}
int _tmain(int argc, _TCHAR* argv[])
{
// observation test 1
int weight_groupA = obj[0] + obj[1] + obj[2] + obj[3];
int weight_groupB = obj[4] + obj[5] + obj[6] + obj[7];
scaleResult = testWeight(weight_groupA, weight_groupB);
if (scaleResult.balanced)
{
// observation test 2
weight_groupA = obj[5] + obj[6] + obj[7];
weight_groupB = obj[8] + obj[9] + obj[10];
scaleResult = testWeight(weight_groupA, weight_groupB);
if (scaleResult.balanced)
{
// observation test 3
scaleResult = testWeight(obj[0], obj[11]);
if (scaleResult.balanced) { std::cout << "error: All objects even weighted\n"; return 0; }
printResult(11, scaleResult.groupB_isHeavier);
}
else if (scaleResult.groupA_isHeavier)
{
// lighter object is in groupB
// observation test 3
scaleResult = testWeight(obj[8], obj[9]);
if (scaleResult.balanced)
printResult(10, false);
else if (obj[8] > obj[9])
printResult(9, false);
else
printResult(8, false);
}
else
{
// heavier ball is in groupA
// observation test 3
scaleResult = testWeight(obj[8], obj[9]);
if (scaleResult.balanced)
printResult(10, true);
else if (obj[8] > obj[9])
printResult(8, true);
else
printResult(9, true);
}
}
else if (scaleResult.groupA_isHeavier)
{
// observation test 2
weight_groupA = obj[4] + obj[5] + obj[0];
weight_groupB = obj[6] + obj[1] + obj[11];
scaleResult = testWeight(weight_groupA, weight_groupB);
if (scaleResult.balanced)
{
// observation test 3
scaleResult = testWeight(obj[2], obj[3]);
if (scaleResult.balanced)
printResult(7, false);
else if (obj[2] > obj[3])
printResult(2, true);
else
printResult(3, true);
}
else if (scaleResult.groupB_isHeavier)
{
// observation test 3
scaleResult = testWeight(obj[4], obj[5]);
if (scaleResult.balanced)
printResult(1, true);
else if (scaleResult.groupA_isHeavier)
printResult(5, false);
else
printResult(4, false);
}
else
{
// observation test 3
scaleResult = testWeight(obj[6], obj[1]);
if (scaleResult.balanced)
printResult(0, true);
else
printResult(6, false);
}
}
else
{
// observation test 2
weight_groupA = obj[0] + obj[1] + obj[4];
weight_groupB = obj[2] + obj[5] + obj[11];
scaleResult = testWeight(weight_groupA, weight_groupB);
if (scaleResult.balanced)
{
// observation test 3
scaleResult = testWeight(obj[6], obj[7]);
if (scaleResult.balanced)
printResult(3, false);
else if (scaleResult.groupA_isHeavier)
printResult(6, true);
else
printResult(7, true);
}
else if (scaleResult.groupB_isHeavier)
{
// observation test 3
scaleResult = testWeight(obj[0], obj[1]);
if (scaleResult.balanced)
printResult(5, true);
else if (scaleResult.groupA_isHeavier)
printResult(1, false);
else
printResult(0, false);
}
else
{
// observation test 3
scaleResult = testWeight(obj[2], obj[3]);
if (scaleResult.balanced)
printResult(4, true);
else
printResult(2, false);
}
}
return 0;
}
[/spoiler]