如果两组数(每组2个数)的积相等:{x1,x2}、{y1,y2},x1*x2=y1*y2,这里x1、y1是1位数,x2、y2是2位数,且x1*x2和y1*y2为三位数,所以x1和y1不能取1,否则乘积不可能为3位数,那么最左边和最右边这两袋就不能放1。遍历所有情况,并且检查x1、x2、y1、y2、以及乘积的每一位都不能一样:
程序代码:
#include <stdio.h>
#define DIGIT_1(number) ((number) % 10)
#define DIGIT_2(number) ((number) / 10 % 10)
#define DIGIT_3(number) ((number) / 100 % 10)
int main(void)
{
int x1, x2, y1, y2, check;
for (x1 = 2; x1 < 10; ++x1) {
for (x2 = 12; x2 < 100; ++x2) {
if (DIGIT_1(x2) == x1 ||
DIGIT_2(x2) == x1)
{
continue;
}
for (y1 = 2; y1 < 10; ++y1) {
if (x1 == y1 ||
DIGIT_1(x2) == y1 ||
DIGIT_2(x2) == y1)
{
continue;
}
for (y2 = 12; y2 < 100; ++y2) {
if (x1 == DIGIT_1(y2) ||
x1 == DIGIT_2(y2) ||
DIGIT_1(x2) == DIGIT_1(y2) ||
DIGIT_1(x2) == DIGIT_2(y2) ||
DIGIT_2(x2) == DIGIT_1(y2) ||
DIGIT_2(x2) == DIGIT_2(y2) ||
y1 == DIGIT_1(y2) ||
y1 == DIGIT_2(y2))
{
continue;
}
if(x1 * x2 == y1 * y2) {
check = x1 * x2;
if (x1 == DIGIT_1(check) ||
x1 == DIGIT_2(check) ||
x1 == DIGIT_3(check) ||
DIGIT_1(x2) == DIGIT_1(check) ||
DIGIT_1(x2) == DIGIT_2(check) ||
DIGIT_1(x2) == DIGIT_3(check) ||
DIGIT_2(x2) == DIGIT_1(check) ||
DIGIT_2(x2) == DIGIT_2(check) ||
DIGIT_2(x2) == DIGIT_3(check) ||
y1 == DIGIT_1(check) ||
y1 == DIGIT_2(check) ||
y1 == DIGIT_3(check) ||
DIGIT_1(y2) == DIGIT_1(check) ||
DIGIT_1(y2) == DIGIT_2(check) ||
DIGIT_1(y2) == DIGIT_3(check) ||
DIGIT_2(y2) == DIGIT_1(check) ||
DIGIT_2(y2) == DIGIT_2(check) ||
DIGIT_2(y2) == DIGIT_3(check) || check < 100)
{
continue;
}
printf("{%d,%d} %d {%d,%d}\n", x1, x2, check, y2, y1);
}
}
}
}
}
return 0;
}
/*
{2,78} 156 {39,4}
{3,58} 174 {29,6}
{4,39} 156 {78,2}
{6,29} 174 {58,3}
*/
初始状态:7 28 196 34 5
显然2 78 156 39 4是移动次数最少的,先swap(2,7),然后swap(9,5),最后swap(4,9),最少三次。
[
本帖最后由 lz1091914999 于 2012-6-5 12:58 编辑 ]