当前位置: 代码迷 >> 综合 >> [蓝桥杯填空]最大乘积
  详细解决方案

[蓝桥杯填空]最大乘积

热度:60   发布时间:2024-01-20 21:38:44.0

把 1~9 这9个数字分成两组,中间插入乘号,

有的时候,它们的乘积也只包含1~9这9个数字,而且每个数字只出现1次。

比如:

984672 * 351 = 345619872

98751 * 3462 = 341875962

9 * 87146325 = 784316925

...

符合这种规律的算式还有很多,请你计算在所有这些算式中,乘积最大是多少?

注意,需要提交的是一个整数,表示那个最大的积,不要填写任何多余的内容。 (只提交乘积,不要提交整个算式)  

使用了next_permutation()

答案:839542176

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
int vis[11];
using namespace std;
int judge(long long x) //判断所得的乘积是否符合只有1-9数字并有且仅出现一次
{for(int i = 0 ; i <= 9; i++)vis[i] = 0;int a;while(x!=0){a = x % 10;vis[a]++;x /= 10;}for(int i = 1; i <= 9; i++){if(vis[i] != 1)return 0;}return 1;
}
int main()
{long long b = 0, c = 0, maxn = 0;int a[]={1,2,3,4,5,6,7,8,9};do{for(int i = 0; i <= 7; i++){ b = 0;c = 0;for(int j = 0; j <= i; j++){b = b * 10 + a[j];}for(int k = i + 1; k <= 8; k++){c = c * 10 + a[k];}if(judge(b*c)){maxn = max(maxn,(b * c));}}}while(next_permutation(a,a+9));cout<<maxn<<endl;	
} 

 

  相关解决方案