用1、2、3、4、5、6、7、8、9这9 个数字,填入 匚中使等式匚匚*匚匚匚 = 匚匚匚匚 成立,每个数字恰好只用一次。

一道算法设计题,我想了半天,只会用快乐蛮力法求解,不知道发发博客会不会有大神说一说更加好的算法~

我的算法思路:
已知解空间,第一个数12-98,第二个数123-987,结果不会超过9999.直接遍历整个解空间去找结果。

按理来说,这遍历的 □□ 与 □□□ 不会超过一半范围的,要不然 □□□□会溢出,我直接用最大的解空间去遍历也可以(主要是这样算解空间清晰一点,缩小解空间没办法让时空复杂度有阶梯性的变化我没改)

#include<iostream>
using namespace std;

//蛮力法
bool check(int numberLeft, int numberRight, int result)
{
	int a[10] = { 0 };
	int tmf = 0;
	a[numberLeft / 10] ++;
	a[numberLeft % 10] ++;
	a[numberRight % 10] ++;
	a[numberRight / 10 % 10] ++;
	a[numberRight / 100] ++;
	a[result % 10] ++;
	a[result / 10 % 10] ++;
	a[result/ 100 % 10] ++;
	a[result / 1000] ++;
	if (a[0] == 0) {
		for (int i = 1; i < 10; i++)
		{
			if (a[i] == 1)
				tmf++;
		}
		if (tmf == 9)
			return true;
		else
			return false;
	}
	else
		return false;
}

bool find() {
	for (int numberLeft = 12; numberLeft < 99; numberLeft++)
		for (int numberRight = 123; numberRight < 988; numberRight++) {
			int result = numberLeft * numberRight;
			if (result > 9999) break;
			if (check(numberLeft, numberRight, result)) cout << numberLeft << " * " << numberRight << " = " << result << endl;
		}
	return true;
}

int main()
{

	find();
	return 0;
}
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AlbertOS

还会有大爷会打钱?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值