1. C/C++

四位吸血鬼

最近在看CSDN上的一位博主【沉默王二】写的文章,还是很有意思的。

突然看到这篇文章https://blog.csdn.net/qing_gee/article/details/50328679

看上去不是很难啊,于是我决定亲手试试,既然是算法嘛,当然用c++啊!

/*
吸血鬼数字是指位数为偶数的数字,可以由一对数字相乘而得到,
而这对数字各包含乘积的一半位数的数字,其中从最初的数字中选取的数字可以任意排序。
以两个0结尾的数字是不允许的,例如,下列数字都是“吸血鬼”数字:
1260 = 21 * 60  1827 = 21 * 87  2187 = 27 * 81
*/

//四位吸血鬼

#include <iostream>
#include <algorithm>
using namespace std;

int main()
{
	char temp[2];
	char buf[5] = {'\n'};

	for (int i = 1000; i < 9999; i++)
	{
		_itoa_s(i, buf, 10);
		int sum = atoi(buf);
		do {
			temp[0] = buf[0];
			temp[1] = buf[1];
			int a = atoi(temp);

			temp[0] = buf[2];
			temp[1] = buf[3];
			int b = atoi(temp);
			if (sum == a * b) {
				printf("%d = %d * %d\n", sum, a, b);
				break;
			}
		} while (next_permutation(buf, buf + 4));
	}
}

1260 = 21 * 60
1395 = 15 * 93
1435 = 35 * 41
1530 = 30 * 51
1827 = 21 * 87
2187 = 27 * 81
6880 = 80 * 86

C:\Users\Michael Jiang\source\repos\吸血鬼\Debug\吸血鬼.exe (进程 14368)已退出,返回代码为: 0。
按任意键关闭此窗口...

直接暴力嘛,不过还有很大的优化空间,哈哈。