1397: [蓝桥杯2018决赛]最大乘积 【一般 / 题目坑】

在这里插入图片描述

注意: 题目可别的坑,乘积后的数也只包含1 ~ 9。
题目一开始没看清,得到了843973902。这里面有0,不是符合题目要求的。

本题解法:
全排列+枚举乘号的位置。

#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int a[9]={
    
    1,2,3,4,5,6,7,8,9};
int main(void)
{
    
    
	ll sum1=0;
	ll sum2=0;
	ll max=-1;
	int index;
	int i,j,k;
	int number1,number2;
	do
	{
    
    
		for(i=0;i<9;i++)
		{
    
    
			sum1=sum2=0;
			for(j=0;j<=i;j++)
			{
    
    
				sum1=sum1*10+a[j];
			}
			for(k=j;k<=8;k++)
			{
    
    
				sum2=sum2*10+a[k];
			}
			int temp=sum1*sum2;
			if(temp>max)
			{
    
    
				bool b[10]={
    
    false};
				while(temp)
				{
    
    
					b[temp%10]=true;
					temp=temp/10;
				}
				int n=0;
				for(int m=1;m<=9;m++)
				{
    
    
					if(b[m]==true)
						n++;
				}
				if(n==9)
					max=sum1*sum2;
			}
		}
	}while(next_permutation(a,a+9));
	printf("%lld\n",max);
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/bettle_king/article/details/115254612