1397: [蓝桥杯2018决赛]最大乘积
题目描述
把 1~9 这9个数字分成两组,中间插入乘号,
有的时候,它们的乘积也只包含1~9这9个数字,而且每个数字只出现1次。
比如:
984672 * 351 = 345619872
98751 * 3462 = 341875962
9 * 87146325 = 784316925
…
符合这种规律的算式还有很多,请你计算在所有这些算式中,乘积最大是多少?
输出
输出一个整数表示答案
来源/分类
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<queue>
#include<vector>
using namespace std;
int s[15];
void init()
{
for(int i=1; i<=9; i++)
s[i]=i;
}
int cmp1(int x)
{
int flog=1;
int book[10];
memset(book,0,sizeof(book));
while(x)
{
book[x%10]++;
x/=10;
}
for(int i=1;i<=9;i++)
{
if(book[i]!=1)
{
flog=0;
break;
}
}
return flog;
}
int cmp(int x)
{
int ans=0,ans1=0;
for(int i=1; i<=x;i++)
ans=ans*10+s[i];
for(int i=x+1;i<=9;i++)
ans1=ans1*10+s[i];
if(cmp1(ans*ans1))
return ans*ans1;
else
return 0;
}
int check()
{
int s=-999;
for(int i=1; i<=9; i++)
{
s=max(s,cmp(i));
}
return s;
}
int main()
{
init();
int ans=-1;
do
{
ans=max(ans,check());
}
while(next_permutation(s+1,s+10));
printf("%d\n",ans);
return 0;
}