USACO Section 1.3 Prime Cryptarithm

没感觉这题和贪心有什么联系,暴力过的,严格按它给的公式来,每个部分乘积必需是三位数,最终和必需是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
posted @ 2011-10-31 08:57  linyvxiang  阅读(177)  评论(0编辑  收藏  举报