USCAO section1.3 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.

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 digits from the set {1,2,3,4,5,6,7,8,9}.

PROGRAM NAME: crypt1

INPUT FORMAT

Line
1:
N, the number of digits that will be used
Line 2: N space separated 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 unique 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

解题思路:用一个五位数数组line[]来存两个乘数的所有可能性,在用一个charge[]数组存三个判断因子的所有可能性并标记(需注意三位数与四位数也要标记为不同,否则后面会出错,我就是这都标记为1后错了一次),而后遍历line[]的三个判断因子,成功则ANS(答案数)加一
如何去五位数:遍历全部五位数找出所有合题意的数。
两个乘数:一个是五位数前三个line[]/100:另一个是五位数后两个line[]%100;
 
 
 
/*
ID:nealgav1
PROG:crypt1
LANG:C++
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 123456
using namespace std;
int line[N];
int charge[N];
int ke;
int pan[12];
int main()
{
  freopen("crypt1.in","r",stdin);
  freopen("crypt1.out","w",stdout);
  int m;
  while(scanf("%d",&m)!=EOF)
  {
    int i;
   memset(pan,0,sizeof(pan));
    for(i=0;i<m;i++)
    {
      scanf("%d",&ke);
      pan[ke]=1;
    }
    int k;
    int M;
    bool flag;
    M=0;
    for(i=10000;i<100000;i++)
    {
      flag=1;
       int z=i;
      while(z)
      {
         k=z%10;
         if(!pan[k])
         {
           flag=0;
           break;
         }
         z/=10;
      }
      if(flag)
      line[M++]=i;
    }
   memset(charge,0,sizeof(charge));
    for(i=100;i<10000;i++)
    {
      flag=1;
      int z=i;
      while(z)
      {
         k=z%10;
         if(!pan[k])
         {
           flag=0;
           break;
         }
         z/=10;
      }
      if(flag)
      {
        if(i>1000)
        charge[i]=1;
        else
        charge[i]=2;
      }

    }
    int ans=0;
    int a,b,c;
    for(i=0;i<M;i++)
    {
      a=line[i]%100;
      line[i]/=100;
      b=a%10;
      c=a/10;
      a*=line[i];
      b*=line[i];
      c*=line[i];
      if(charge[a]==1&&charge[b]==2&&charge[c]==2)
      {
        ans++;
      }
    }
    printf("%d\n",ans);
  }
}

 
 
Prime Cryptarithm Russ Cox

The constraints of this problem are small enough that we can just try all possible products of 3 digit * 2 digit numbers, and look to see if all the correct digits are used.

The function "isgood" checks that a number is composed only of acceptable digits, and "isgoodprod" checks that all the lines of the multiplication are composed of acceptable digits.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

int isgooddigit[10];	/* isgooddigit[d] is set if d is an acceptable digit
*/

/* check that every decimal digit in "n" is a good digit,
   and that it has the right number "d" of digits. */
int
isgood(int n, int d)
{
    if(n == 0)
		return 0;

    while(n) {
	if(!isgooddigit[n%10])
	    return 0;
	n /= 10;
        d--;
    }

    if( d == 0 )
       return 1;
    else
       return 0;
}

/* check that every product line in n * m is an okay number */
int
isgoodprod(int n, int m)
{
    if(!isgood(n,3) || !isgood(m,2) || !isgood(n*m,4))
	return 0;

    while(m) {
	if(!isgood(n*(m%10),3))
	    return 0;
	m /= 10;
    }
    return 1;
}

void
main(void)
{
    int i, j, n, nfound;
    FILE *fin, *fout;

    fin = fopen("crypt1.in", "r");
    fout = fopen("crypt1.out", "w");
    assert(fin != NULL && fout != NULL);

    for(i=0; i<10; i++) {
        isgooddigit[i] = 0;
    }
    fscanf(fin, "%d", &n);
    for(i=0; i<n; i++) {
	fscanf(fin, "%d", &j);
	isgooddigit[j] = 1;
    }

   nfound = 0;
   for(i=100; i<1000; i++)
	for(j=10; j<100; j++)
	    if(isgoodprod(i, j))
		nfound++;

   fprintf(fout, "%d\n", nfound);
   exit(0);
}

 
 
 
 
 
USER: Neal Gavin Gavin [nealgav1]
TASK: crypt1
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.011 secs, 4308 KB]
   Test 2: TEST OK [0.011 secs, 4308 KB]
   Test 3: TEST OK [0.011 secs, 4308 KB]
   Test 4: TEST OK [0.000 secs, 4308 KB]
   Test 5: TEST OK [0.000 secs, 4308 KB]
   Test 6: TEST OK [0.011 secs, 4308 KB]
   Test 7: TEST OK [0.011 secs, 4308 KB]

All tests OK.

Your program ('crypt1') produced all correct answers! This is yoursubmission #2 for this problem. Congratulations!

Here are the test data inputs:

------- 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

Keep up the good work!

原文链接: https://www.cnblogs.com/nealgavin/archive/2012/07/11/3206048.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    USCAO section1.3 Prime Cryptarithm(感觉思路挺好)

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/54970

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月9日 上午6:06
下一篇 2023年2月9日 上午6:06

相关推荐