Prime Cryptarithm

  最多就9个数,直接一层一层枚举就行。注意判断出现的数的每一位都要出现在集合中。

/*
ID: like_091
PROG: crypt1
LANG: C++
*/
#include<iostream>
#include<fstream>
using namespace std;
//判断tem中的每一位都在含有n个元素的集合x中
bool fun(int tem, int x[], int n)
{
	while (tem > 0)
	{
		bool r = true;
		for (int i = 0; i < n; i++)
			if (x[i] == (tem % 10))
			{
				r = false;
				break;
			}
		if (r)return false;
		tem /= 10;
	}
	return true;
}
int main()
{
	ifstream cin("crypt1.in");
	ofstream cout("crypt1.out");
	int n, d[10], sum = 0;
	cin>>n;
	for (int i = 0; i < n; i++)
		cin>>d[i];
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			for (int k = 0; k < n; k++)
				for (int l = 0; l < n; l++)
					for (int m = 0; m < n; m++)
					{
						int a = d[i] * 100 + d[j] * 10 + d[k];
						int b = d[l] * 10 + d[m];
						//如果a和b中的每一位都在集合中
						if (fun(a, d, n) && fun(b, d, n))
						{
							//如果a乘以b中的每一位都在集合中且是三位数
							if (a * d[m] < 1000 && fun(a * d[m], d, n) && a * d[l] < 1000 && fun(a * d[l], d, n))
							{
								//a*b是四位数且每一位数都在集合中
								if (a * b < 10000 && fun(a * b, d, n))
										sum++;
							}
						}
					}
	cout<<sum<<endl;
	return 0;
}

原文链接: https://www.cnblogs.com/neulike/archive/2011/02/21/1959522.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    Prime Cryptarithm

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/21305

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月7日 下午11:20
下一篇 2023年2月7日 下午11:20

相关推荐