Input
Thefirstlineofinputgivesthenumberofcases,N(1≤N≤100).Ntestcasesfollow.Eachtestcasecontainstwointergeraandbseparatedbyasinglespaceinaline.
Output
Foreachtestcase,printGCDandLCMseparatedbyasinglespaceinaline.
SampleInput
2
86
50003000
SampleOutput
224
100015000
#include
intgetgcd(inta,intb)
{
intgcd;
intt1,t2;
t1=a;
t2=b;
gcd=t1%t2;
while(gcd!
=0)
{
t1=t2;
t2=gcd;
gcd=t1%t2;
}returnt2;
}intmain(){intn;
inta,b;while(scanf("%d",&n)!
=EOF){while(n--)
{scanf("%d%d",&a,&b);printf("%d%d\n",getgcd(a,b),a*b/(getgcd(a,b)));
}}return0;
}
排序后求位置处的数
Sortit⋯DescriptionThereisadatabase,partychenwantyoutosortthedatabase'sdataintheorderfromtheleastup
tothegreatestelement,thendothequery:
"Whichelementisi-thbyitsvalue?
"-withibeinganaturalnumberinarangefrom1toN.
Itshouldbeabletoprocessquicklyquerieslikethis.
Input
Thestandardinputoftheproblemconsistsoftwoparts.Atfirst,adatabaseiswritten,andthenthere'sasequenceofqueries.Theformatofdatabaseisverysimple:
inthefirstlinethere'sanumberN(1<=N<=100000),inthenextNlinestherearenumbersofthedatabaseoneineachlineinanarbitraryorder.Asequenceofqueriesiswrittensimplyaswell:
inthefirstlineofthesequenceanumberofqueriesK(1<=K<=100)iswritten,andinthenextKlinestherearequeriesoneineachline.Thequery"Whichelementisi-thbyitsvalue?
"iscodedbythenumberi.
Output
TheoutputshouldconsistofKlines.Ineachlinethereshouldbeananswertothecorrespondingquery.Theanswertothequery"i"isanelementfromthedatabase,whichisi-thbyitsvalue(intheorderfromtheleastuptothegreatestelement).
SampleInput5
7
121
123
7
121
3
3
2
5
SampleOutput
121
7
123
#include
#include
usingnamespacestd;
intnum[100010];
intpos[105];
intmain()
{
intn;
inti;
intk;
while(scanf("%d",&n)!
=EOF)
{
for(i=1;i<=n;i++)
scanf("%d",&num[i]);
scanf("%d",&k);
for(i=1;i<=k;i++)
scanf("%d",&pos[i]);
sort(num+1,num+1+n);
for(i=1;i<=k;i++)
printf("%d\n",num[pos[i]]);
}
return0;
}
*路由器连接
HubConnectionplan
Description
Partychenisworkingassystemadministratorandisplanningtoestablishanewnetworkinhiscompany.TherewillbeNhubsinthecompany,theycanbeconnectedtoeachotherusingcables.
Sinceeachworkerofthecompanymusthaveaccesstothewholenetwork,eachhubmustbeaccessiblebycablesfromanyotherhub(withpossiblysomeintermediatehubs).
Sincecablesofdifferenttypesareavailableandshorteronesarecheaper,itisnecessarytomakesuchaplanofhubconnection,thatthecostisminimal.partychenwillprovideyouallnecessaryinformationaboutpossiblehubconnections.Youaretohelppartychentofindthewaytoconnecthubssothatallaboveconditionsaresatisfied.
Input
Thefirstlineoftheinputcontainstwointegernumbers:
N-thenumberofhubsinthenetwork(2<=N<=1000)andM-thenumberofpossiblehubconnections(1<=M<=15000).Allhubsarenumberedfrom1toN.ThefollowingMlinescontaininformationaboutpossibleconnections-thenumbersoftwohubs,whichcanbeconnectedandthecablecostrequiredtoconnectthem.costisapositiveintegernumberthatdoesnotexceed106.Therewillalwaysbeatleastonewaytoconnectallhubs.
Output
Outputtheminimizecostofyourhubconnectionplan.
SampleInput
46
121
131
142
231
341
241
SampleOutput
3
#include
#includeusingnamespacestd;structEdge{
inta,b;
intcost;
}E[15010];
intTree[1010];
intfindRoot(intx)
{if(Tree[x]==-1)
returnx;
else
{
inttmp=findRoot(Tree[x]);
Tree[x]=tmp;
returntmp;
}
}
boolCmp(Edgea,Edgeb)
{
returna.cost}
intmain()
{
intn;
intm;
inti;
while(scanf("%d",&n)!
=EOF)
{
scanf("%d",&m);for(i=1;i<=m;i++)scanf("%d%d%d",&E[i].a,&E[i].b,&E[i].cost);sort(E+1,E+1+m,Cmp);//排序for(i=1;i<=n;i++)
Tree[i]=-1;
intans=0;for(i=1;i<=m;i++)
{
inta=findRoot(E[i].a);
intb=findRoot(E[i].b);if(a!
=b)
{
Tree[a]=b;ans+=E[i].cost;
}
}printf("%d\n",ans);
}
return0;
}
*编译原理
PrinciplesofCompiler
Description
AfterlearntthePrinciplesofCompiler,partychenthoughtthathecansolveasimpleexpressionproblem.Sohegiveyoustringsoflessthan100characterswhichstrictlyadheretothefollowinggrammar(giveninEBNF):
A:
='('B')'|'x'.
B:
=AC.
C:
={'+'A}.
Canyousolvethemtoo?
Input
Thefirstlineofinputgivesthenumberofcases,N(1≤N≤100).Ntestcasesfollow.
ThenextNlineswilleachcontainastringasdescribedabove.
Output
elseoutput
Foreachtestcase,iftheexpressionisadapttotheEBNFaboveoutput“Good”
SampleInput
3
(x)
(x+(x+x))
()(x)
SampleOutput
Good
Good
Bad
#include
#include#include#include#include#include#include#include#include#include
usingnamespacestd;
charex[110];intindex;boolA();
boolB();
boolC();
boolA()
{
if(ex[index]=='x')
{
index++;
while(ex[index]=='')index++;returntrue;
}
if(ex[index]=='(')
{
index++;
while(ex[index]=='')index++;if(B()&&ex[index]==')')
{
index++;
while(ex[index]=='')index++;returntrue;
}
}
returnfalse;
}
boolB()
{
returnA()&&C();
}
boolC()
{
while(ex[index]=='+')
{
index++;
while(ex[index]=='')index++;
//returnA();
if(!
A())
returnfalse;
}
returntrue;
}
intmain()
{
intN;
scanf("%d",&N);
getchar();
while(N--)
{
gets(ex);
index=0;
printf("%s\n",A()&&ex[index]=='\0'?
"Good":
"Bad");
}
return0;
}
*分开连接
SeparateConnections
Description
Partychenareanalyzingacommunicationsnetworkwithatmost18nodes.Characterinamatrixi,j(i,jboth0-based,asmatrix[i][j])denoteswhethernodesiandjcancommunicate('Y'foryes,'N'forno).Assuminganodecannotcommunicatewithtwonodesatonce,returnthemaximumnumberofnodesthatcancommunicatesimultaneously.Ifnodeiiscommunicatingwithnodejthennodejiscommunicatingwithnodei.
Input
Thefirstlineofinputgivesthenumberofcases,N(1≤N≤100).Ntestcasesfollow.
Ineachtestcase,thefirstlineisthenumberofnodesM(1≤M≤18),thenthereareagridbyM*Mdescribledthematrix.
Output
Foreachtestcase,outputthemaximumnumberofnodesthatcancommunicatesimultaneously
SampleInput
2
5
NYYYY
YNNNN
YNNNN
YNNNN
YNNNN
5
NYYYY
YNNNN
YNNNY
YNNNY
YNYYN
SampleOutput
2