猜算式



用暴力破解法,再用个数组去记录每个数字出现的次数,一个数字出现超过两次则不符合题意;


代码:

public class Main {

	public static boolean check(int num, int[] s) {
		while (num != 0) {
			if (s[num % 10] == 2) { // 如果这个数字已经出现过两次,则返回false,不输出
				return false;
			}
			s[num % 10]++;
			num /= 10;
		}
		return true;
	}

	public static void main(String[] args) {
		for (int i = 100; i <= 999; i++) {
			for (int j = 100; j <= 999; j++) {
				int[] s = new int[10];
				int a = i * (j % 10);// i*j的个位
				int b = i * (j / 10 % 10);// i*j的十位
				int c = i * (j / 100);// i*j的百位
				int sum = i * j;
				if (a > 999 || b > 999 || c > 999 || sum > 99999 || a < 100 || b < 100 || c < 100 || sum < 10000) {
					continue;
				}
				if (check(i, s) && check(j, s) && check(a, s) && check(b, s) && check(c, s) && check(sum, s)) {
					System.out.println(sum);
				}
			}
		}
	}

}


第二种方法:

import java.util.Arrays;

public class Main {

	public static boolean check(String str) {
		char[] c = str.toCharArray();// 将字符串转为字符数组
		Arrays.sort(c);// 将字符数组从小到大排序
		if ("00112233445566778899".equals(new String(c))) {
			return true;
		}
		return false;
	}

	public static void main(String[] args) {
		String str = "";
		for (int i = 100; i <= 999; i++) {
			for (int a = 0; a < 10; a++) {
				for (int b = 0; b < 10; b++) {
					for (int c = 0; c < 10; c++) {
						int sum = i * (a * 100 + b * 10 + c);
						if (sum > 10000 && sum < 100000) {
							str = "" + i + a + b + c + i * a + i * b + i * c + sum;// 将所有数字拼接为一个字符串
							if (str.length() == 20 && check(str)) {
								System.out.println(sum);
							}
						}
					}
				}
			}
		}
	}
}

答案:40096

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值