USACO--Prime Cryptarithm

Prime Cryptarithm

The following cryptarithm is a multiplication problem that can be solved by substituting digits from a specified set of N digits into the positions marked with *. If the set of prime digits {2,3,5,7} is selected, the cryptarithm is called a PRIME CRYPTARITHM.

      * * *
   x    * *
    -------
      * * *         <-- partial product 1
    * * *           <-- partial product 2
    -------
    * * * *
Digits can appear only in places marked by `*'. Of course, leading zeroes are not allowed.

The partial products must be three digits long, even though the general case (see below) might have four digit partial products. 

********** Note About Cryptarithm's Multiplication ************ 
In USA, children are taught to perform multidigit multiplication as described here. Consider multiplying a three digit number whose digits are 'a', 'b', and 'c' by a two digit number whose digits are 'd' and 'e':

[Note that this diagram shows far more digits in its results than
the required diagram above which has three digit partial products!]

          a b c     <-- number 'abc'
        x   d e     <-- number 'de'; the 'x' means 'multiply'
     -----------
p1      * * * *     <-- product of e * abc; first star might be 0 (absent)
p2    * * * *       <-- product of d * abc; first star might be 0 (absent)
     -----------
      * * * * *     <-- sum of p1 and p2 (e*abc + 10*d*abc) == de*abc

Note that the 'partial products' are as taught in USA schools. The first partial product is the product of the final digit of the second number and the top number. The second partial product is the product of the first digit of the second number and the top number.

Write a program that will find all solutions to the cryptarithm above for any subset of supplied non-zero single-digits.

PROGRAM NAME: crypt1

INPUT FORMAT

Line 1: N, the number of digits that will be used
Line 2: N space separated non-zero digits with which to solve the cryptarithm

SAMPLE INPUT (file crypt1.in)

5
2 3 4 6 8

OUTPUT FORMAT

A single line with the total number of solutions. Here is the single solution for the sample input:

      2 2 2
    x   2 2
     ------
      4 4 4
    4 4 4
  ---------
    4 8 8 4

SAMPLE OUTPUT (file crypt1.out)

1

這道題的題意還真是無奈,後來才知道要求兩個數的積必須小於10000,而且中間的積(上邊的數分別乘以下邊那個數的個位和十位)都要是三位數.感覺這不是貪心=_=  !

思路:我開了一個數組,輸入後的n個數,在對應的hash_arr中這個下表的值爲true,然後從100-999和10-99進行枚舉,條件滿足,就加一

代碼如下:

#include<iostream>
#include<cstdio>
#include<cstring>
#define MAXN 105
using namespace std;

bool hash_arr[10];  //輸入的n個數,將其作爲數組下標,輸入哪個數,對應數組中值爲true

int is_exist(int n)  //判斷組成這個數的幾個數存是否存在
{
	bool ok=true;
	int trans[20],len=0;
	memset(trans,0,sizeof(trans));
	while(n)   //先把這個數分開,存在trans數組中
	{
		trans[len++]=(n%10);
		n/=10;
	}
	for(int i=0;i<len;i++)
	{
		if(!hash_arr[ trans[i] ]) {ok=false;break;}   //如果這個數爲下表的數組中的值爲0,就說明不存在,就結束
	}
	return ok;
}

int main()
{
    freopen("crypt1.in","r",stdin);
    freopen("crypt1.out","w",stdout);
	int n;
	scanf("%d",&n);
	memset(hash_arr,0,sizeof(hash_arr));
	for(int i=0;i<n;i++)
	{
		int tem;
		scanf("%d",&tem);
		hash_arr[tem]=true;//對應數組中的值爲true
	}
	int a,b,c,d,e,sum=0;   //a是上邊乘數,b是下邊的乘數,c是a乘以b的十位數的積,d是a乘以b的個位上數的積,e是a*b
	for(int i=100;i<=999;i++)
	{
		for(int j=10;j<=99;j++)
		{
			a=i;b=j;c=i*(j/10);d=i*(j%10);e=i*j;
			if( is_exist(a) && is_exist(b) && is_exist(c) && is_exist(d) && is_exist(e) && e<=9999 && c<1000 && d<1000) sum++;
			//如果每個數都存在而且符合條件,sum的值+1
		}
	}
	printf("%d\n",sum);
	return 0;
}

發佈了50 篇原創文章 · 獲贊 2 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章