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
/*
ID: conicoc1
LANG: C
TASK: crypt1
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int IsInUnion(int X,int Y,int *S)
{
while(S[X]>0)
X=S[X];
while(S[Y]>0)
Y=S[Y];
return X==Y;
}
int JudgeNumber(int Sum,int t,int *S)
{
if(Sum>9999 ||Sum<100)
return 0;
int k;
if(Sum>999)
k=1000;
else
k=100;
for(;k!=0;k/=10)
{
// printf("%d\n",Sum);
if(!IsInUnion(Sum/k,t,S)||Sum%k<k/10)
break;
Sum%=k;
}
return k==0;
}
int main()
{
FILE *fin,*fout,*ftxt;
fin=fopen("crypt1.in","r");
fout=fopen("crypt1.out","w");
int A[10]={-1};
int S[10]={-1};
int N;
fscanf(fin,"%d",&N);
int i,temp=0;
for(i=0;i<N;i++)
{
fscanf(fin,"%d",&A[i]);
// printf("%2d",A[i]);
S[A[i]]=temp;
temp=A[i];
}
int q,w,e,r,t;
int Sum1,Sum2,Sum3;
int count=0;
for(q=N-1;q>=0;q--)
{
for(w=N-1;w>=0;w--)
{
for(e=N-1;e>=0;e--)
{
for(r=N-1;r>=0;r--)
{
for(t=N-1;t>=0;t--)
{
Sum1=A[t]*A[e]+A[t]*A[w]*10+A[t]*A[q]*100;
Sum2=A[r]*A[e]+A[r]*A[w]*10+A[q]*A[r]*100;
Sum3=Sum1+Sum2*10;
if(Sum2>999||Sum1>999||Sum3>9999)
continue;
if(JudgeNumber(Sum1,A[t],S)&&JudgeNumber(Sum2,A[t],S)&&JudgeNumber(Sum3,A[t],S))
{
printf("%d+%d=%d:\n",Sum1,Sum2*10,Sum3);
count++;
// printf("Yes\n");
}
}
}
}
}
}
// printf("IsInUnion(2,8,S)=%d",IsInUnion(2,8,S));
fprintf(fout,"%d\n",count);
return 0;
}
毫无违和感啊,可是自己还是写的好复杂。。一开始忽略了中间存在0的情况。。
#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);
}