(usaco)牛式 Prime Cryptarithm

题目传送门sxazr
本题读懂题意就好了;
判断一下乘出来的数位数是否符合;
判断一下数中是否由输入中给出的数组成;
暴力枚举啦
代码

#include <iostream>
#include <cstdio>
using namespace std;
int ans,n,a[10];
bool q(int x)
{
	while(x)
	{
		if(!a[x%10])
		  return 0;
		x/=10;
	}
	return 1;
}
bool zr(int x,int y)
{
	int g=x*(y%10),b=x*(y/10),c=x*y;
	if(g>999||b>999||c>9999) return 0;
	if(q(g)&&q(b)&&q(c)&&q(x)&&q(y))
	  return 1;
	return 0;
}
int main()
{
	cin>>n;
	for(int x,i=1;i<=n;i++)
	  scanf("%d",&x),a[x]=1;
	for(int i=100;i<1000;i++)
	  for(int j=10;j<100;j++){
	  	if(zr(i,j))
	  	  ans++;
	  }
	cout<<ans;
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42920137/article/details/88778118
今日推荐