USACO Section 1.3 Prime Cryptarithm

本文介绍了一个针对Crypt1问题的暴力求解算法实现。该算法遵循题目给出的规则,确保每位数有效且最终结果符合特定条件。通过遍历所有可能组合来找到满足条件的四位数乘积。

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

没感觉这题和贪心有什么联系,暴力过的,严格按它给的公式来,每个部分乘积必需是三位数,最终和必需是4位数,而且不能有前导零

华丽的缩进~~

/*
	ID:linyvxi1
	PROB:crypt1
	LANG:C++
*/
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
bool flag[10]={false};
bool check(int n)
{
	if(n==0)
		return flag[0];
	while(n){
		if(!flag[n%10])
			return false;
		n/=10;
	}
	return true;
}

int main()
{
	FILE*	fin=fopen("crypt1.in","r");
	FILE*	fout=fopen("crypt1.out","w");

	int n,num[10];
	fscanf(fin,"%d",&n);
	int i,j,k,p,q;
	for(i=0;i<n;i++){
		fscanf(fin,"%d",&num[i]);
		flag[num[i]]=true;
	}
	sort(num,num+n);
	int count=0;
	for(i=0;i<n;i++)
		for(j=0;j<n;j++)
			for(k=0;k<n;k++){
				for(p=0;p<n;p++)
					for(q=0;q<n;q++){
						if(num[i]!=0&&num[p]!=0){
							int temp1=(100*num[i]+10*num[j]+num[k])*num[q];
							int temp2=(100*num[i]+10*num[j]+num[k])*num[p];
							if(100<=temp1&&temp1<1000&&100<=temp2&&temp2<1000){
								if(check(temp1)&&check(temp2)&&check(10*temp1+temp2)){
									if((10*temp1+temp2)>1000&&(10*temp1+temp2)<10000)
										count++;
								}
							}
						}
					}
			}
	fprintf(fout,"%d\n",count);
}
	

  

------- test 1 ----
5
2 3 4 6 8
------- test 2 ----
4
2 3 5 7
------- test 3 ----
1
1
------- test 4 ----
7
4 1 2 5 6 7 3
------- test 5 ----
8
9 1 7 3 5 4 6 8
------- test 6 ----
6
1 2 3 5 7 9
------- test 7 ----
9
1 2 3 4 5 6 7 8 9

转载于:https://www.cnblogs.com/yangce/archive/2011/10/31/2229757.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值