实在想不通,大家帮忙看看,急
有12个铁球,目前知道里面有一只球的重量与其它的不同,要求只用3次比较找出该球。注意,该球究竟是比其他球重还是轻是未知的。请编程序实现该问题。
2011-03-18 23:26

2011-03-19 09:11
2011-03-19 10:13
2011-03-19 10:16
2011-03-19 11:51

2011-03-19 12:00
2011-03-19 12:16
程序代码:/*
*有12个铁球,目前知道里面有一只球的重量与其它的不同,要求只用3次比较找出该球。
*注意,该球究竟是比其他球重还是轻是未知的。请编程序实现该问题。
*/
#include <stdio.h>
#define LENGTH 12//数组的长度
int test[LENGTH+1] = {0};
int Compare_to_two(int unkown_x, int unkown_y, int standard_z);
int Compare_array(int dim_x, int dim_y, int num);
void Find_error();
bool Check_array();
void Get_input();
void Show_different_ball( int unkown_x, int unkown_y, int standard_z);
int main()
{
bool flag = false;
while ( !flag )
{
Get_input();
flag = Check_array();
}
Find_error();
return 0;
}
/*
*从控制台输入12个数
*/
void Get_input()
{
int i = 1;
printf("\t从控制台输入11个相同的数和1个不同的数以空格隔开\n");
while( LENGTH+1 != i )
{
scanf("%d", test+i);
++i;
}
return;
}
/*
*检查输入是否合格 合格则返回true 否则false
*/
bool Check_array()
{
int i = 2, same_count = 0, different_count = 0;
while ( LENGTH+1 != i )
{
if( test[1] == test[i] )
{
++same_count;
}
else
{
++different_count;
}
++i;
}
if( (10 == same_count && 1 == different_count) || (0 == same_count && 11 == different_count) )
{
return true;
}
return false;
}
/*
*从12个球中查找有问题的那个并取出来
*/
void Find_error()
{
//{1,2,3,4}{5,6,7,8}{9,10,11,12}
int result = Compare_array( 1, 5, 4 );
int sub_result;
int sum_1 = test[1] + test[6] + test[7];
int sum_2 = test[2] + test[8] + test[9];
if( 0 == result )
{//表示{1,2,3,4}=={5,6,7,8} 问题存在于{9,10,11,12}
sub_result = Compare_to_two( 9, 10, 1 );
if( 0 == sub_result )
{//问题在 11 12 中
Show_different_ball( 11, 12, 1 );
}
else//( -1 == sub_result )
{//问题在9 10 中
Show_different_ball( 9, 10, 1 );
}
return;
}
else if( -1 == result )
{//表示{9,10,11,12} 是标准的 问题在比较的两组当中的一组中
//{1,2,3,4} {5,6,7,8}把3,4,5拿掉
//{1,2} {6,7,8} 2和6,7调换
//{1, 6, 7} {2, 8} 把9放进去
//{1, 6, 7} {2, 8, 9}
if( sum_1 == sum_2 )
{//问题在于 3 4 5
if( test[3] == test[4] )
{//5不同
printf("\t第5号球与其他不同\n");
}
else if( test[3] > test[4] )
{
printf("\t第3号球与其他不同\n");
}
else
{
printf("\t第4号球与其他不同\n");
}
return;
}
else if( sum_1 < sum_2 )
{//问题在于交换的那些中2, 6, 7
if( test[6] == test[7] )
{
printf("\t第2号球与其他不同\n");
}
else if( test[6] > test[7] )
{
printf("\t第7号球与其他不同\n");
}
else
{
printf("\t第6号球与其他不同\n");
}
}
else
{//问题在1, 8
if( test[1] == test[9] )
{
printf("\t第8号球与其他不同\n");
}
else
{
printf("\t第1号球与其他不同\n");
}
}
}
else
{//表示{9,10,11,12} 是标准的 问题在比较的两组当中的一组中
if( sum_1 == sum_2 )
{//问题在于 3 4 5
if( test[3] == test[4] )
{//5不同
printf("\t第5号球与其他不同\n");
}
else if( test[3] < test[4] )
{
printf("\t第3号球与其他不同\n");
}
else
{
printf("\t第4号球与其他不同\n");
}
return;
}
else if( sum_1 < sum_2 )
{//问题在1, 8
if( test[1] == test[9] )
{
printf("\t第8号球与其他不同\n");
}
else
{
printf("\t第1号球与其他不同\n");
}
}
else
{//问题在于交换的那些中2, 6, 7
if( test[6] == test[7] )
{
printf("\t第2号球与其他不同\n");
}
else if( test[6] < test[7] )
{
printf("\t第7号球与其他不同\n");
}
else
{
printf("\t第6号球与其他不同\n");
}
}
}
return;
}
/*
*比较两个数组的大小 相等返回 0, 左边的大 -1, 右边的大1
*/
int Compare_array(int dim_x, int dim_y, int num)
{
int sum_x = 0;//存放总值dim_x
int sum_y = 0;//存放总值dim_y
int index = 0;
while( num != index )
{
sum_x += test[dim_x + index];
++index;
}
index = 0;
while( num != index )
{
sum_y += test[dim_y + index];
++index;
}
if( sum_x == sum_y )
{
return 0;
}
else if( sum_x > sum_y )
{
return -1;
}
else
{
return 1;
}
}
/*
*两个球未知球 和 两个标准求相比较
*相等返回0 未知重返回-1 标准重返回1
*/
int Compare_to_two(int unkown_x, int unkown_y, int standard_z)
{
int xy = test[unkown_x] + test[unkown_y];
if( xy == 2*standard_z )
{
return 0;
}
else if( xy > 2*standard_z )
{
return -1;
}
else
{
return 1;
}
}
/*
*输出两个球中 有问题的那个球出来
*/
void Show_different_ball( int unkown_x, int unkown_y, int standard_z)
{
if( test[unkown_x] == test[standard_z] )
{
printf("\t第%d号球与其他不同\n", unkown_y);
return;
}
else
{
printf("\t第%d号球与其他不同\n", unkown_x);
return;
}
}
2011-03-20 11:04
2011-03-20 18:44
2011-03-20 21:20