华东师范大学计算机机试真题Word文档格式.docx
《华东师范大学计算机机试真题Word文档格式.docx》由会员分享,可在线阅读,更多相关《华东师范大学计算机机试真题Word文档格式.docx(73页珍藏版)》请在冰豆网上搜索。
while(n--)
intan=0;
scanf("
%d%d"
a,&
b);
sum=a+b;
while(sum)
{
an++;
sum/=10;
}
printf("
%d\n"
an++);
}
return0;
大写改小写
Capitalize
Writeaprogramwhichreplaceallthelower-caselettersofagiventextwiththecorrespondingcaptitalletters.
Atextincludinglower-caseletters,periods,andspace.
OutputTheconvertedtext.
welcometoeastchinanormaluniversity.
WELCOMETOEASTCHINANORMALUNIVERSITY.
string.h>
charstr[1000];
intl;
while(gets(str))
l=strlen(str);
inti;
for(i=0;
i<
l;
i++)
if(str[i]>
='
a'
&
str[i]<
z'
)
printf("
%c"
str[i]-32);
else
str[i]);
\n"
);
素数对
PrimesPair
Wearrangethenumbersbetween1andN(1<
=N<
=10000)inincreasingorderanddecreasingorderlikethis:
123456789...N
N...987654321
Twonumbersfacedeachotherformapair.YourtaskistocomputethenumberofpairsPsuchthatbothnumbersinthepairsareprime.
Thefirstlineofinputgivesthenumberofcases,C(1≤C≤100).Ctestcasesfollow.
EachtestcaseconsistsofanintegerNinoneline.
Foreachtestcase,outputP.
4
1
7
51
6
boolprime[10005];
voidinit()
intj;
prime[0]=prime[1]=false;
//不是素数
prime[2]=true;
//是素数
for(i=3;
=10005;
i+=2)
prime[i]=true;
prime[i+1]=false;
//不是素数除0和2之外的偶数都不是素数
if(prime[i]==true)//是素数
j=i+i;
while(j<
=10005)
prime[j]=false;
j+=i;
intc;
init();
//初始化
c)!
while(c--)
scanf("
n);
intsum=0;
for(i=2;
=n/2;
if(prime[i]==true&
prime[n+1-i]==true)
sum++;
sum*=2;
if(n%2==1)//n为奇数
if(prime[n/2+1]==true)
sum+=1;
sum);
求最大公约数和最小公倍数
GCDandLCM
Writeaprogramwhichcomputesthegreatestcommondivisor(GCD)andtheleastcommonmultiple(LCM)ofgivenaandb(0<
a,b≤44000).
Eachtestcasecontainstwointergeraandbseparatedbyasinglespaceinaline.
Foreachtestcase,printGCDandLCMseparatedbyasinglespaceinaline.
86
50003000
224
100015000
intgetgcd(inta,intb)
intgcd;
intt1,t2;
t1=a;
t2=b;
gcd=t1%t2;
while(gcd!
=0)
t1=t2;
t2=gcd;
returnt2;
%d%d\n"
getgcd(a,b),a*b/(getgcd(a,b)));
排序后求位置处的数
Sortit…
Thereisadatabase,partychenwantyoutosortthedatabase’sdataintheorderfromtheleastuptothegreatestelement,thendothequery:
"
Whichelementisi-thbyitsvalue?
"
-withibeinganaturalnumberinarangefrom1toN.
Itshouldbeabletoprocessquicklyquerieslikethis.
Thestandardinputoftheproblemconsistsoftwoparts.Atfirst,adatabaseiswritten,andthenthere'
sasequenceofqueries.Theformatofdatabaseisverysimple:
inthefirstlinethere'
sanumberN(1<
=N<
=100000),inthenextNlinestherearenumbersofthedatabaseoneineachlineinanarbitraryorder.Asequenceofqueriesiswrittensimplyaswell:
inthefirstlineofthesequenceanumberofqueriesK(1<
=K<
=100)iswritten,andinthenextKlinestherearequeriesoneineachline.Thequery"
iscodedbythenumberi.
TheoutputshouldconsistofKlines.Ineachlinethereshouldbeananswertothecorrespondingquery.Theanswertothequery"
i"
isanelementfromthedatabase,whichisi-thbyitsvalue(intheorderfromtheleastuptothegreatestelement).
5
121
123
5
algorithm>
usingnamespacestd;
intnum[100010];
intpos[105];
intk;
for(i=1;
=n;
num[i]);
k);
=k;
pos[i]);
sort(num+1,num+1+n);
num[pos[i]]);
*路由器连接
HubConnectionplan
Partychenisworkingassystemadministratorandisplanningtoestablishanewnetworkinhiscompany.TherewillbeNhubsinthecompany,theycanbeconnectedtoeachotherusingcables.Sinceeachworkerofthecompanymusthaveaccesstothewholenetwork,eachhubmustbeaccessiblebycablesfromanyotherhub(withpossiblysomeintermediatehubs).
Sincecablesofdifferenttypesareavailableandshorteronesarecheaper,itisnecessarytomakesuchaplanofhubconnection,thatthecostisminimal.partychenwillprovideyouallnecessaryinformationaboutpossiblehubconnections.Youaretohelppartychentofindthewaytoconnecthubssothatallaboveconditionsaresatisfied.
Thefirstlineoftheinputcontainstwointegernumbers:
N-thenumberofhubsinthenetwork(2<
=1000)andM-thenumberofpossiblehubconnections(1<
=M<
=15000).Allhubsarenumberedfrom1toN.ThefollowingMlinescontaininformationaboutpossibleconnections-thenumbersoftwohubs,whichcanbeconnectedandthecablecostrequiredtoconnectthem.costisapositiveintegernumberthatdoesnotexceed106.Therewillalwaysbeatleastonewaytoconnectallhubs.
Outputtheminimizecostofyourhubconnectionplan.
46
121
131
142
231
341
241
structEdge{
intcost;
}E[15010];
intTree[1010];
intfindRoot(intx)
if(Tree[x]==-1)
returnx;
inttmp=findRoot(Tree[x]);
Tree[x]=tmp;
returntmp;
boolCmp(Edgea,Edgeb)
returna.cost<
b.cost;
intm;
m);
=m;
%d%d%d"
E[i].a,&
E[i].b,&
E[i].cost);
sort(E+1,E+1+m,Cmp);
//排序
Tree[i]=-1;
intans=0;
inta=findRoot(E[i].a);
intb=findRoot(E[i].b);
if(a!
=b)
Tree[a]=b;
ans+=E[i].cost;
ans);
*编译原理
PrinciplesofCompiler
AfterlearntthePrinciplesofCompiler,partychenthoughtthathecansolveasimpleexpressionproblem.Sohegiveyoustringsoflessthan100characterswhichstrictlyadheretothefollowinggrammar(giveninEBNF):
A:
='
('
B'
)'
|'
x'
.
B:
=AC.
C:
={'
+'
A}.
Canyousolvethemtoo?
ThenextNlineswilleachcontainastringasdescribedabove.
Foreachtestcase,iftheexpressionisadapttotheEBNFaboveoutput“Good”,elseoutput“Bad”.
(x)
(x+(x+x))
()(x)
Good
Bad
#include<
cstdio>
cstring>
cstdlib>
vector>
cmath>
iostream>
functional>
string>
map>
cctype>
charex[110];
intindex;
boolA();
boolB();
boolC();
boolA()
if(ex[index]=='
index++;
while(ex[index]=='
'
)index++;
returntrue;
if(B()&
ex[index]=='
returnfalse;
boolB()
returnA()&
C();
boolC()
//returnA();
if(!
A())
intN;
N);
getchar();
while(N--)
gets(ex);
index=0;
%s\n"
A()&
\0'
?
Good"
:
Bad"
return0;
*分开连接
SeparateConnections
Partychenareanalyzingacommunicationsnetworkwithatmost18nodes.Characterinamatrixi,j(i,jboth0-based,asmatrix[i][j])denoteswhethernodesiandjcancommunicate('
Y'
foryes,'
N'
forno).Assuminganodecannotcommunicatewithtwonodesatonce,returnthemaximumnumberofnodesthatcancommunicatesimultaneously.Ifnodeiiscommunicatingwithnodejthennodejiscommunicatingwithnodei.
Ineachtestcase,thefirstlineisthenumberofnodesM(1≤M≤18),thenthereareagridbyM*Mdescribledthematrix.
Foreachtestcase,outputthemaximumnumberofnodesthatcancommunicatesimultaneously
NYYYY
YNNNN
YNNNY
YNYYN
Hint
Thefirsttestcase:
Allcommunicationsmustoccurwithnode0.Sincenode0canonlycommunicatewith1nodeatatime,theoutputvalueis2.
Thesecondtestcase:
Inthissetup,wecanletnode0communicatewithnode1,andnode3communicatewithnode4.
#i