USCAO section1.3 Prime Cryptarithm(感觉思路挺好)
2012-07-11 17:46
387 查看
PrimeCryptarithm ThefollowingcryptarithmisamultiplicationproblemthatcanbesolvedbysubstitutingdigitsfromaspecifiedsetofNdigitsintothepositionsmarkedwith*.Ifthesetofprimedigits{2,3,5,7}isselected,thecryptarithmiscalledaPRIMECRYPTARITHM. [code]*** x** ------- ***<--partialproduct1 ***<--partialproduct2 ------- ****Digitscanappearonlyinplacesmarkedby`*'.Ofcourse,leadingzeroesarenotallowed.
Notethatthe'partialproducts'areastaughtinUSAschools.Thefirstpartialproductistheproductofthefinaldigitofthesecondnumberandthetopnumber.Thesecondpartialproductistheproductofthefirstdigitofthesecondnumberandthetopnumber.
Writeaprogramthatwillfindallsolutionstothecryptarithmaboveforanysubsetofdigitsfromtheset{1,2,3,4,5,6,7,8,9}.
PROGRAMNAME:crypt1
INPUTFORMAT
Line 1: | N,thenumberofdigitsthatwillbeused |
Line2: | Nspaceseparateddigitswithwhichtosolvethecryptarithm |
SAMPLEINPUT(filecrypt1.in)
5 23468
OUTPUTFORMAT
Asinglelinewiththetotalnumberofuniquesolutions.Hereisthesinglesolutionforthesampleinput:222 x22 ------ 444 444 --------- 4884
SAMPLEOUTPUT(filecrypt1.out)
1
[/code]
解题思路:用一个五位数数组line[]来存两个乘数的所有可能性,在用一个charge[]数组存三个判断因子的所有可能性并标记(需注意三位数与四位数也要标记为不同,否则后面会出错,我就是这都标记为1后错了一次),而后遍历line[]的三个判断因子,成功则ANS(答案数)加一
如何去五位数:遍历全部五位数找出所有合题意的数。
两个乘数:一个是五位数前三个line[]/100:另一个是五位数后两个line[]%100;
/*
ID:nealgav1PROG:crypt1LANG:C++
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
#defineN123456
usingnamespacestd;
intline
;
intcharge
;
intke;
intpan[12];
intmain()
{
freopen("crypt1.in","r",stdin);
freopen("crypt1.out","w",stdout);
intm;
while(scanf("%d",&m)!=EOF)
{
inti;
memset(pan,0,sizeof(pan));
for(i=0;i<m;i++)
{
scanf("%d",&ke);
pan[ke]=1;
}
intk;
intM;
boolflag;
M=0;
for(i=10000;i<100000;i++)
{
flag=1;
intz=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;
intz=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;
}
}
intans=0;
inta,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==2&&charge[c]==2)
{
ans++;
}
}
printf("%d\n",ans);
}
}
[/code]
[b]PrimeCryptarithm
RussCoxTheconstraintsofthisproblemaresmallenoughthatwecanjusttryallpossibleproductsof3digit*2digitnumbers,andlooktoseeifallthecorrectdigitsareused.
Thefunction"isgood"checksthatanumberiscomposedonlyofacceptabledigits,and"isgoodprod"checksthatallthelinesofthemultiplicationarecomposedofacceptabledigits.
[code]#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<assert.h>
intisgooddigit[10]; /*isgooddigit[d]issetifdisanacceptabledigit
*/
/*checkthateverydecimaldigitin"n"isagooddigit,
andthatithastherightnumber"d"ofdigits.*/
int
isgood(intn,intd)
{
if(n==0)
return0;
while(n){
if(!isgooddigit[n%10])
return0;
n/=10;
d--;
}
if(d==0)
return1;
else
return0;
}
/*checkthateveryproductlineinn*misanokaynumber*/
int
isgoodprod(intn,intm)
{
if(!isgood(n,3)||!isgood(m,2)||!isgood(n*m,4))
return0;
while(m){
if(!isgood(n*(m%10),3))
return0;
m/=10;
}
return1;
}
void
main(void)
{
inti,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);
}
[/code]
USER:NealGavinGavin[nealgav1]
TASK:crypt1LANG:C++
Compiling...
Compile:OK
Executing...
Test1:TESTOK[0.011secs,4308KB]
Test2:TESTOK[0.011secs,4308KB]
Test3:TESTOK[0.011secs,4308KB]
Test4:TESTOK[0.000secs,4308KB]
Test5:TESTOK[0.000secs,4308KB]
Test6:TESTOK[0.011secs,4308KB]
Test7:TESTOK[0.011secs,4308KB]
AlltestsOK.
Yourprogram('crypt1')producedallcorrectanswers!Thisisyour
submission#2forthisproblem.Congratulations!
Herearethetestdatainputs:
-------test1----
5 23468-------test2----
4
2357
-------test3----
11-------test4----
7
4125673
-------test5----
8
91735468
-------test6----
6
123579
-------test7----
9
123456789
Keepupthegoodwork!
相关文章推荐
- USCAO section1.3 Prime Cryptarithm(感觉思路挺好)
- USCAO-Section 1.3 Prime Cryptarithm
- USACO Section1.3 Prime Cryptarithm 解题报告
- USACO Section 1.3 Prime Cryptarithm
- USACO-Section1.3 Prime Cryptarithm
- USCAO section 1.3 Mixing Milk
- USCAO section 1.3 Mixing Milk
- USACO: Section 1.3 -- PROB Prime Cryptarithm
- USACO Section 1.3 : Prime Cryptarithm
- USCAO section1.3 Barn Repair
- USACO Section 1.3: Prime Cryptarithm
- USACO-Section1.3 Prime Cryptarithm
- USCAO section1.3 Barn Repair
- USCAO-Section 1.3 Wormholes
- [USACO]Section 1.3 Prime Cryptarithm
- USACO-Section 1.3 Prime Cryptarithm(枚举)
- USACO - Chapter1 Section 1.3 - Prime Cryptarithm
- USACO Section 1.3 Prime Cryptarithm
- USACO-Section 1.3 Prime Cryptarithm[...]