第一次发贴,做一做第1题。附件下载后改名为1.cpp
解体算法最原始的方法:穷举 (循环次数绝对不是10!)
耗时大概1S左右吧, 没有做时间测试。
虽然,B F G 可以推算出 为 9, 5 ,0 ,但还是最原始的方法让他们参加循环。
{
ABCDE
DFG
DFG
---------
XYZDE
推算B, 因为 X = A + 1 , 则 B 必须为 9。
(2 * FG ) % 100 必须为0 且F != G ,则 F = 5 G = 0;
}
下面给出算法:
//1.cpp
// hkice August 17 2007
// I use bcc5.5 free command line compiler
// c:> bcc32 1.cpp
// c:> 1.exe
//Result:abcdefgxyz
// 2978650314
//=======================
// 29786
// 850
// + 850
//-----------------------
// 31486
#include <iostream.h>
int main()
{
for (int z = 1; z < 10; z++)
{
for (int y = 0; y < 10; y++)
{
if (y == z)
continue;
for (int x = 1; x < 10; x++)
{
if (x == y || x == z)
continue;
for (int g = 0; g < 10; g++)
{
if (g == x || g == y || g == z)
continue;
for (int f = 0; f < 10; f++)
{
if (f == g || f == x || f == y || f == z)
continue;
for (int e = 0; e < 10; e++)
{
if (e == f || e == g || e == x || e == y || e == z)
continue;
for (int d = 1; d < 10; d++)
{
if (d == e || d == f || d == g || d == x || d == y || d == z)
continue;
for (int c = 0; c < 10; c++)
{
if (c == d || c == e || c == f || c == g || c == x || c == y || c == z)
continue;
for (int b = 0; b < 10; b++)
{
if (b == c || b == d || b == e || b == f || b == g || b == x || b == y || b == z)
continue;
for (int a = 1; a < 10; a++)
{
if (a == b || a== c || a == d || a == e || a == f || a == g || a == x || a == y || a == z)
continue;
//cout <<"Test" << a << b << c << d << e << f << g << x << y << z << endl;
int abcde = a * 10000 + b * 1000 + c * 100 + d * 10 + e;
int dfg = d * 100 + f * 10 + g;
int xyzde = x* 10000 + y * 1000 + z* 100 + d * 10 + e;
if ( abcde + 2 * dfg - xyzde == 0)
{
cout << "Result:" << 'a' << 'b' << 'c' << 'd' << 'e' << 'f' << 'g'
<< 'x' << 'y' << 'z' << endl;
cout << " " << a << b << c << d << e << f << g << x << y << z << endl;
cout << "=======================" <<endl;
cout << " " << abcde <<endl;
cout << " " << dfg <<endl;
cout << " + " << dfg <<endl;
cout << "-----------------------" << endl;
cout << " " << xyzde << endl;
}
}
}
}
}
}
}
}
}
}
}
return 0;
}
[此贴子已经被作者于2007-8-17 15:30:55编辑过]