超级传送门:http://projecteuler.net/problem=38
枚举每个排列,分别判断是否合法,最大的排列即为答案。
注意从918273645开始枚举即可,因为这是题目中告诉的一组解,要找比这组解更大的才行。
先枚举n的值,从2到9,再枚举要乘的那个数,记为x,枚举x的位数即可,1~4,x的位数不可能大于4,如果大于4,那么2x的位数必然也大于4,组合起来长度会大于9,故不合法。
比如918273645,可以这样枚举x:先枚举x=9,然后x=91,然后x=918,然后x=9182,就够了。
枚举完x就可以对x试乘2~n,每次乘完与原串匹配一下,记下原串匹配成功的位置cntMatchIndex,发现某处不匹配即刻终止对此x的试乘。
最后如果cntMatchIndex为9,则该串合法。
代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
char num[] = "918273645";
int isValid(char *num)
{
char buffer[15];
int x = 0;
int cntMatchIndex = 0;
for (int n = 2; n <= 9; n++)
{
x = 0;
for (int i = 0; i < 4; i++)
{
x *= 10;
x += num[i] - '0';
cntMatchIndex = i + 1;
int ok = 1;
for (int j = 2; j <= n && ok; j++)
{
int y = j * x;
sprintf(buffer, "%d", y);
int len = strlen(buffer);
if (cntMatchIndex + len > 9)
break;
for (int bIndex = 0; bIndex < len; bIndex++)
{
if (buffer[bIndex] != num[cntMatchIndex++])
{
ok = 0;
break;
}
}
}
if (ok && cntMatchIndex == 9)
return 1;
}
}
return 0;
}
int main()
{
char ans[15];
do
{
if (isValid(num))
strcpy(ans, num);
} while (next_permutation(num, num + 9));
printf("%s\n", ans);
return 0;
}