您的位置:首页 > 其它

USACO section 1.5 Superprime Rib(深搜)

2012-08-13 14:24 495 查看

SuperprimeRib

ButcheringFarmerJohn'scowsalwaysyieldsthebestprimerib.Youcantellprimeribsbylookingatthedigitslovinglystampedacrossthem,onebyone,byFJandtheUSDA.FarmerJohnensuresthatapurchaserofhisprimeribsgetsreallyprimeribsbecausewhenslicedfromtheright,thenumbersontheribscontinuetostayprimerightdowntothelastrib,e.g.: [code]7331


Thesetofribsdenotedby7331isprime;thethreeribs733areprime;thetworibs73areprime,and,ofcourse,thelastrib,7,isprime.Thenumber7331iscalledasuperprimeoflength4.
WriteaprogramthatacceptsanumberN1<=N<=8ofribsandprintsallthesuperprimesofthatlength.
Thenumber1(byitself)isnotaprimenumber.

PROGRAMNAME:sprime

INPUTFORMAT

AsinglelinewiththenumberN.

SAMPLEINPUT(filesprime.in)

4

OUTPUTFORMAT

ThesuperprimeribsoflengthN,printedinascendingorderoneperline.

SAMPLEOUTPUT(filesprime.out)

2333
2339
2393
2399
2939
3119
3137
3733
3739
3793
3797
5939
7193
7331
7333
7393


[/code]




/*
ID:nealgav1
PROG:sprime
LANG:C++
*/
#include<cstdio>
#include<cmath>
intm;
boolisprim(intnum)//判断质数
{
if(num==2)return1;
if(num&1)
{
intn=(int)sqrt(num);
for(inti=3;i<=n;i+=2)
if(!(num%i))
return0;
return1;
}
return0;
}
voiddfs(intnum,intn)
{
intans;
if(m==n)
{
printf("%d\n",num);
return;
}
for(inti=1;i<=9;i+=2)
{
ans=num*10+i;
if(isprim(ans))
dfs(ans,n+1);
}
}
intmain()
{
freopen("sprime.out","w",stdout);
freopen("sprime.in","r",stdin);
scanf("%d",&m);
dfs(2,1);dfs(3,1);dfs(5,1);dfs(7,1);
}







USER:NealGavinGavin[nealgav1]
TASK:sprime
LANG:C++

Compiling...
Compile:OK

Executing...
Test1:TESTOK[0.000secs,3344KB]
Test2:TESTOK[0.000secs,3344KB]
Test3:TESTOK[0.000secs,3344KB]
Test4:TESTOK[0.000secs,3344KB]
Test5:TESTOK[0.000secs,3344KB]

AlltestsOK.
Yourprogram('sprime')producedallcorrectanswers!Thisisyour
submission#2forthisproblem.Congratulations!


Herearethetestdatainputs:

-------test1----
4-------test2----
5
-------test3----
6
-------test4----
7
-------test5----
8

Keepupthegoodwork!


Thanksforyoursubmission!

SuperPrimeRibRussCox

Weusearecursivesearchtobuildsuperprimesoflengthnfromsuperprimesoflengthn-1byaddinga1,3,7,or9.(Numbersendinginanyotherdigitaredivisibleby2or5.)Sincetherearesofewnumbersbeingtested,asimpleprimalitytestsuffices.

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

FILE*fout;

int
isprime(intn)
{
inti;

if(n==2)
return1;

if(n%2==0)
return0;

for(i=3;i*i<=n;i+=2)
if(n%i==0)
return0;

return1;
}

/*printallsprimespossiblebyaddingndigitdigitstothenumbern*/
void
sprime(intn,intndigit)
{
if(ndigit==0){
fprintf(fout,"%d\n",n);
return;
}

n*=10;
if(isprime(n+1))
sprime(n+1,ndigit-1);
if(isprime(n+3))
sprime(n+3,ndigit-1);
if(isprime(n+7))
sprime(n+7,ndigit-1);
if(isprime(n+9))
sprime(n+9,ndigit-1);
}

void
main(void)
{
intn;
FILE*fin;

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

fscanf(fin,"%d",&n);

sprime(2,n-1);
sprime(3,n-1);
sprime(5,n-1);
sprime(7,n-1);
exit(0);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: