USACO 1.3.5 Prime Cryptarithm

本文通过一个具体的编程实例,详细解析了暴力算法的实现过程。通过对输入数据的筛选与组合,使用多重循环来遍历所有可能的情况,并对每种情况进行判断,确保结果只包含合法的数字组合。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目分析:暴力

/*
ID:wconvey
PROG:crypt1
LANG:C++
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int cmp(int x,int y)
{
	return x<y;
}
int main()
{
	freopen("crypt1.in","r",stdin);
	freopen("crypt1.out","w",stdout);

	int n;
	bool flag[10];
	scanf("%d",&n);
	int f[4],g[4],h[5];
	int x,y,z,a,b,arr[10];

	memset(flag,0,sizeof(flag));
	memset(f,0,sizeof(f));
	memset(g,0,sizeof(g));
	memset(h,0,sizeof(h));

	for(int i=1;i<=n;i++)
	{
		scanf("%d",&arr[i]);
		flag[arr[i]]=1;
	}

	sort(arr+1,arr+1+n,cmp);

	int ans=0,temp;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			for(int k=1;k<=n;k++)
				for(int p=1;p<=n;p++)
					for(int q=1;q<=n;q++)
					{
						x=arr[i],y=arr[j],z=arr[k];
						a=arr[p],b=arr[q];

						f[3]=(b*z)%10;
						if(flag[f[3]]==0)
							continue;
						temp=b*z/10;
						temp+=b*y;
						f[2]=temp%10;
						temp=temp/10+b*x;
						if(temp>arr[n])
							continue;
						f[1]=temp;

						g[3]=(a*z)%10;
						temp=(a*z)/10;
						temp+=a*y;
						g[2]=temp%10;
						temp=temp/10+a*x;
						if(temp>arr[n])
							continue;
						g[1]=temp;

                        h[3]=(f[2]+g[3])%10;
						temp=(f[2]+g[3])/10;
						temp+=f[1]+g[2];
						h[2]=temp%10;
						temp=temp/10+g[1];
						if(temp>arr[n])
							continue;
						h[1]=temp;

						if(flag[f[1]]+flag[f[2]]+flag[f[3]]+flag[g[1]]+flag[g[2]]+flag[g[3]]+
							flag[h[1]]+flag[h[2]]+flag[h[3]]==9)
							ans++;

					}
	printf("%d\n",ans);
	//system("pause");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值