第九届蓝桥杯(国赛)——最大乘积

【问题描述】

把 1~9 这9个数字分成两组,中间插入乘号,
有的时候,它们的乘积也只包含1~9这9个数字,而且每个数字只出现1次。
比如:
984672 * 351 = 345619872
98751 * 3462 = 341875962
9 * 87146325 = 784316925

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

【答案提交】
注意,需要提交的是一个整数,表示那个最大的积,不要填写任何多余的内容。


答案:839542176


  1. 先生成全排列数字
  2. 将全排列数字分离成两个数字
  3. 判断两数之积是否包含 1~9,且每个数字只出现一次
import java.math.BigInteger;
import java.util.*;
public class Main {
	static int t=0;static int nub=0;static int max=-99999999;
	static int max1=0;
 	public static void main(String[] args) {
		// TODO Auto-generated method stu
 		Scanner scanner=new Scanner(System.in);
 		
 		method("123456789".toCharArray(), 0);
 		System.out.println(max);
 	}
 	public static void method(char []a,int b ) {
			if (b==a.length) {
//				分割字符串
			String string=String.valueOf(a);
				for (int j = 1; j <= a.length; j++) {
					String string2=string.substring(0,j);
					String string3=string.replace(string2, "0");
					int t=Integer.parseInt(string2);
					int nub=Integer.parseInt(string3);
					max1=t*nub;	
						if (max<max1&&method2(max1)&&method3(max1)) {
							max=max1;
							
						}
					}
		}
//			全排列
 		for (int i = b; i < a.length; i++) {
			char temp=a[i];
			a[i]=a[b];
			a[b]=temp;
			method(a, b+1);
			temp=a[i];
			a[i]=a[b];
			a[b]=temp;
		}
	}
// 	去重复
 	public static boolean method2(int max) {
 		String str=String.valueOf(max);
 		int[] a = new int[128];
		for(int i=0; i<str.length(); i++){
			int c = (int)str.charAt(i);
			if(a[c]>0)//大于零说明是第二次碰到了,出现重复
				return false;
			else
				a[c]++;
		}
		return true;
	}
// 	去0
 	public static boolean method3(int max) {
		String string=String.valueOf(max);
		if (string.contains("0")) {
			return false;	}
		return true;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值