把球任意进行编号,并且分两堆,各5个。
可以记住这两堆的成员号码。用天平称量。如果相同,结束。
如果不同,重的堆为上限,轻的堆为下限。
列出10中取5的组合全部可能,这可在网上找到源代码。
依次从组合可能中取出一个可能,
如果该可能的质量在上下限之间,
则把该可能与剩下的五个作为新的上下限。
如果新的上下限相同,结束。
如果该可能的质量不在上下限之间,就取组合中的下一个可能。
组合中所有可能都试完时。上下限就是结果。
如何测定质量在上下限之间?
记住的上下限成员之一与取出的“可能”进行称量。
有个小技巧,即要把要比较的两个成员中相同编号的小球排除,
这样才能保证有足够的球来代表两个要比较的成员。
下面就是C源代码,编译后即可执行,执行语法命令后跟空白隔开的10个数字。
./a.out 100 99 1 5 42 42 42 5 42 42
#include <stdio.h>
#include <stdlib.h>
#define q 176
int j[]={
0,1,2,3,4, 5,6,7,8,9, 0,1,2,3,5, 4,6,7,8,9, 0,1,2,4,5, 3,6,7,8,9, 0,1,3,4,5, 2,6,7,8,9,
0,2,3,4,5, 1,6,7,8,9, 1,2,3,4,5, 0,6,7,8,9, 0,1,2,3,6, 4,5,7,8,9, 0,1,2,4,6, 3,5,7,8,9,
0,1,3,4,6, 2,5,7,8,9, 0,2,3,4,6, 1,5,7,8,9, 1,2,3,4,6, 0,5,7,8,9, 0,1,2,5,6, 3,4,7,8,9,
0,1,3,5,6, 2,4,7,8,9, 0,2,3,5,6, 1,4,7,8,9, 1,2,3,5,6, 0,4,7,8,9, 0,1,4,5,6, 2,3,7,8,9,
0,2,4,5,6, 1,3,7,8,9, 1,2,4,5,6, 0,3,7,8,9, 0,3,4,5,6, 1,2,7,8,9, 1,3,4,5,6, 0,2,7,8,9,
2,3,4,5,6, 0,1,7,8,9, 0,1,2,3,7, 4,5,6,8,9, 0,1,2,4,7, 3,5,6,8,9, 0,1,3,4,7, 2,5,6,8,9,
0,2,3,4,7, 1,5,6,8,9, 1,2,3,4,7, 0,5,6,8,9, 0,1,2,5,7, 3,4,6,8,9, 0,1,3,5,7, 2,4,6,8,9,
0,2,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,8,
0,3,4,7,9, 1,2,5,6,8, 1,3,4,7,9, 0,2,5,6,8, 2,3,4,7,9, 0,1,5,6,8, 0,1,5,7,9, 2,3,4,6,8,
0,2,5,7,9, 1,3,4,6,8, 1,2,5,7,9, 0,3,4,6,8, 0,3,5,7,9, 1,2,4,6,8, 1,3,5,7,9, 0,2,4,6,8
};
int i[10];
h(int *m, int *n)
{ int a;
*m = 0;
for(a=0; a < 5; a++) *m += i[n[a]];
}
p()
{ int a, d;
int c, f[5];
int b, e[5];
int g, k[5];
for(a=0; a < 5; a++)
{ f[a] = 2*a;
e[a] = 2*a+1;
}
h((int *)&c, (int *)&f);
h((int *)&b, (int *)&e);
if(c != b)
{ for(d=0; d < q; d++)
{
for(a=0; a < 5; a++) k[a] = *(j+d*10+a);
h((int *)&g, (int *)&k);
if(((g > b) && (g < c)) || ((g < b) && (g > c)))
{
for(a=0; a < 5; a++)
{ f[a] = *(j+d*10+a);
e[a] = *(j+d*10+a+5);
}
h((int *)&c, (int *)&f);
h((int *)&b, (int *)&e);
if(c == b) break;
}
}
}
printf("%d ", b);
for(a=0; a < 5; a++) printf("%d ", i[e[a]]); printf("\n");
printf("%d ", c);
for(a=0; a < 5; a++) printf("%d ", i[f[a]]); printf("\n");
printf("相差 %d\n", (b > c)? b-c : c-b);
}
int main(int o, char *l[])
{ int a, d;
if(o < 10)
{ printf("Usage : %s 100 99 42 42 42 42 42 5 5 1\n", l[0]);
exit(0);
}
for(a=0; a < 10; a++)
{ sscanf(l[a+1], "%d", &d);
i[a] = d;
}
p();
} |