1. Solve this cryptic equation, realizing of course that values for M and E could be interchanged. No leading zeros are allowed.
WWWDOT - GOOGLE = DOTCOM
虽然找到O+L = O 或者 O - 9 -1 = O的规律,但是还是认为自己大脑再行穷举路径太多了。所以重用了自己过去的代码解决了。
//
验证函数
bool IsCorrent( int * dest)
... {
int wwwdot = dest[0]*(100000+10000+1000)+dest[1]*100+dest[2]*10+dest[3];
int google = dest[4]*(100000+100)+dest[2]*(10000+1000)+dest[5]*10+dest[6];
int dotcom = dest[1]*100000+dest[2]*(10000+10)+dest[3]*1000+dest[7]*100+dest[8];
if(wwwdot - google == dotcom)
...{
return true;
}
else
...{
return false;
}
}
// 求子集函数
void All( int * src, int currentIndex, int len, int * dest, int num)
... {
if(currentIndex == len)
return;
dest[num++] = src[currentIndex++];
if(num == 9)
...{
Perm(dest,0,8,9);
}
All(src,currentIndex,len,dest,num);
All(src,currentIndex,len,dest,num-1);
}
// 求排列函数
void Perm( int * src, int current, int lesslen, int len)
... {
if(current == lesslen)
...{
if(IsCorrent(src))
...{
for(int i = 0; i < len; i++)
...{
cout << src[i]<<" ";
}
cout << endl;
}
}
for(int i = current; i < len; i++)
...{
swap(src[i],src[current]);
Perm(src,current+1,lesslen,len);
swap(src[i],src[current]);
}
}
bool IsCorrent( int * dest)
... {
int wwwdot = dest[0]*(100000+10000+1000)+dest[1]*100+dest[2]*10+dest[3];
int google = dest[4]*(100000+100)+dest[2]*(10000+1000)+dest[5]*10+dest[6];
int dotcom = dest[1]*100000+dest[2]*(10000+10)+dest[3]*1000+dest[7]*100+dest[8];
if(wwwdot - google == dotcom)
...{
return true;
}
else
...{
return false;
}
}
// 求子集函数
void All( int * src, int currentIndex, int len, int * dest, int num)
... {
if(currentIndex == len)
return;
dest[num++] = src[currentIndex++];
if(num == 9)
...{
Perm(dest,0,8,9);
}
All(src,currentIndex,len,dest,num);
All(src,currentIndex,len,dest,num-1);
}
// 求排列函数
void Perm( int * src, int current, int lesslen, int len)
... {
if(current == lesslen)
...{
if(IsCorrent(src))
...{
for(int i = 0; i < len; i++)
...{
cout << src[i]<<" ";
}
cout << endl;
}
}
for(int i = current; i < len; i++)
...{
swap(src[i],src[current]);
Perm(src,current+1,lesslen,len);
swap(src[i],src[current]);
}
}
最后:
WDOTGLECM分别为7 5 8 9 1 0 6 4 3或7 5 8 9 1 0 3 4 6因为E和M可以互换,所以正解.....