数算B上机期末考题.docx

上传人:b****5 文档编号:5395843 上传时间:2022-12-15 格式:DOCX 页数:14 大小:24.24KB
下载 相关 举报
数算B上机期末考题.docx_第1页
第1页 / 共14页
数算B上机期末考题.docx_第2页
第2页 / 共14页
数算B上机期末考题.docx_第3页
第3页 / 共14页
数算B上机期末考题.docx_第4页
第4页 / 共14页
数算B上机期末考题.docx_第5页
第5页 / 共14页
点击查看更多>>
下载资源
资源描述

数算B上机期末考题.docx

《数算B上机期末考题.docx》由会员分享,可在线阅读,更多相关《数算B上机期末考题.docx(14页珍藏版)》请在冰豆网上搜索。

数算B上机期末考题.docx

数算B上机期末考题

3:

最短网络

∙View

∙Submit

∙Statistics

∙Clarify

总TimeLimit:

 

1000ms

 

MemoryLimit:

 

65536kB

Description

农民约翰被选为他们镇的镇长!

他其中一个竞选承诺就是在镇上建立起互联网,并连接到所有的农场。

当然,他需要你的帮助。

约翰已经给他的农场安排了一条高速的网络线路,他想把这条线路共享给其他农场。

为了用最小的消费,他想铺设最短的光纤去连接所有的农场。

你将得到一份各农场之间连接费用的列表,你必须找出能连接所有农场并所用光纤最短的方案。

每两个农场间的距离不会超过100000。

  

Input

农场的个数,N(3<=N<=100)

后来的行包含了一个N*N的矩阵,表示每个农场之间的距离。

理论上,他们是N行,每行由N个用空格分隔的数组成,实际上,他们限制在80个字符,因此,某些行会紧接着另一些行。

当然,对角线将会是0,因为不会有线路从第i个农场到它本身。

Output

只有一个输出,其中包含连接到每个农场的光纤的最小长度。

SampleInput

4

04921

40817

98016

2117160

SampleOutput

28

#include

#include

#defineINF1<<30

#include

usingnamespacestd;

intn;

intgraph[101][101]={0};

intmst;

boolmark[101]={0};

structdist

{

intlength;

intpre;

};

intminvertex(dist*&d);

 

voidprim(ints)

{

//intmasttag=0;

mst=0;

dist*d;

d=newdist[n];

for(inti=0;i

{

mark[i]=0;

d[i].length=INF;

d[i].pre=s;

}

d[s].length=0;

mark[s]=1;

intv=s;

inti;

for(i=0;i

{

for(inte=0;e

{

if(e==v)continue;

if(mark[e]==0&&(d[e].length>graph[v][e]))

{

d[e].length=graph[v][e];

d[e].pre=v;

}

}

intx=minvertex(d);

mark[x]=1;

mst+=d[x].length;

v=x;

}

}

intminvertex(dist*&d)

{

inti,v;

for(i=0;i

{

if(mark[i]==0)

{

v=i;

break;

}

}

for(i=0;i

{

if(mark[i]==0&&(d[i].length

v=i;

}

returnv;

}

intmain()

{

cin>>n;

for(inti=0;i

for(intj=0;j

{

intt;

cin>>t;

graph[i][j]=t;

}

prim(0);

cout<

return0;

}

5:

BinaryTreeinText

∙View

∙Submit

∙Statistics

∙Clarify

总TimeLimit:

 

1000ms

 

MemoryLimit:

 

65536kB

Description

Asthediagramshownabove,everynodeisrepresentedbyaletterinthebinarytreeandthelettersaredifferentfromeachother.Itcanberepresentedbelow.

 

A

-B

--*

--C

-D

--E

---*

---F

 

Inthetext:

1)Everyletterrepresentedanode.Thelinenumberofthenodeisthesameasthelinenumberoftheletter.Therootisatthefirstline.

2)Thenumberofcharacter‘-‘attheleftsideoftheletterrepresentsthelevelofthenode.(Theleveloftherootis0)

3)Ifthelinenumberofanode,whichisnotrootandthelevelofwhichisi,isn,thelevelofitsfathernodeisi-1,thelinenumberofitsfatherislessthannanditsfatheristheonewiththesmallestgapbetweenitslinenumberandn.

4)Ifanode,whoselevelisiandlinenumberisn,hastwochildnodes,itsleftchildnodeistheoneinlinen+1anditsrightchildnodeisthefirstoneatleveli+1inthosewithlinenumbermorethann+1.

5)Ifanode,whoselevelisi,isinlinenhasleftchildnodeanddoesn’thaverightchildnode,thenextlineofwhichisi+1character‘-‘andacharacter‘*’.

 

Givenatreeintext,pleaseoutputthepreorder,postorderandinfixordertraversal.

Input

Thefistlineisthenumberoftreesn, 

followedbyntreesandtheendofeachoneis‘0’.The‘0’isnotapartofthetree.

Everytreehasnomorethan100nodes.

Output

Outputthreelinesforeverytree:

thepreorder,postorderandinfixorderresults.

Thereisanemptylinebetweentrees.

SampleInput

2

A

-B

--*

--C

-D

--E

---*

---F

0

A

-B

-C

0

SampleOutput

ABCDEF

CBFEDA

BCAEFD

ABC

BCA

BAC

#include

#include

#include

usingnamespacestd;

inttempl=-1;

chartempc=0;

classnode

{

public:

charc;

node*lch,*rch;

node()

{

c=0;

lch=NULL;

rch=NULL;

}

};

classtree

{

public:

node*root;

node*build(intlevel)

{

inti=0;

node*now=newnode;

if(tempc==0)

{

charc;

cin>>c;

while(c=='-')

{

if(c!

='-')

break;

i++;

cin>>c;

}

if(i<=level)

{

tempc=c;

templ=i;

returnNULL;

}

else

{

if(c=='*')

returnNULL;

now->c=c;

now->lch=build(i);

now->rch=build(i);

returnnow;

}

}

elseif(templ<=level&&templ!

=-1)

{

returnNULL;

}

else

{

now->c=tempc;

i=templ;

templ=-1;

tempc=0;

now->lch=build(i);

now->rch=build(i);

returnnow;

}

}

voidprefix(node*p)

{

if(p==NULL)

return;

cout<c;

prefix(p->lch);

prefix(p->rch);

}

voidinfix(node*p)

{

if(p==NULL)

return;

infix(p->lch);

cout<c;

infix(p->rch);

}

voidpostfix(node*p)

{

if(p==NULL)

return;

postfix(p->lch);

postfix(p->rch);

cout<c;

}

};

intmain()

{

intn;

cin>>n;

inttc;

while(n--)

{

treet;

t.root=t.build(-1);

t.prefix(t.root);

cout<

t.postfix(t.root);

cout<

t.infix(t.root);

cout<

cout<

templ=-1;

tempc=0;

}

return0;

}

4:

UbiquitousReligions

∙View

∙Submit

∙Statistics

∙Clarify

总TimeLimit:

 

5000ms

 

MemoryLimit:

 

65536kB

Description

Therearesomanydifferentreligionsintheworldtodaythatitisdifficulttokeeptrackofthemall.Youareinterestedinfindingouthowmanydifferentreligionsstudentsinyouruniversitybelievein. 

Youknowthattherearenstudentsinyouruniversity(0

Input

Theinputconsistsofanumberofcases.Eachcasestartswithalinespecifyingtheintegersnandm.Thenextmlineseachconsistsoftwointegersiandj,specifyingthatstudentsiandjbelieveinthesamereligion.Thestudentsarenumbered1ton.Theendofinputisspecifiedbyalineinwhichn=m=0.

Output

Foreachtestcase,printonasinglelinethecasenumber(startingwith1)followedbythemaximumnumberofdifferentreligionsthatthestudentsintheuniversitybelievein.

SampleInput

109

12

13

14

15

16

17

18

19

110

104

23

45

48

58

00

SampleOutput

Case1:

1

Case2:

7

Hint

Hugeinput,scanfisrecommended.

Source

AlbertaCollegiateProgrammingContest2003.10.18

 

#include

#include

#defineINF1<<30

#include

usingnamespacestd;

 

structnode

{

intfather;

intcount;

}student[50001];

 

intfindx(inti)

{

if(student[i].father==i)

returni;

student[i].father=findx(student[i].father);

returnstudent[i].father;

}

 

voiduni(inti,intj)

{

intifa=findx(i);

intjfa=findx(j);

if(ifa!

=jfa)

{

if(student[ifa].count>=student[jfa].count)

{

student[jfa].father=ifa;

student[ifa].count+=student[jfa].count;

}

else

{

student[ifa].father=jfa;

student[jfa].count+=student[ifa].count;

}

}

}

intmain()

{

intn,m;

cin>>n>>m;

inttimes=1;

while(n!

=0&&m!

=0)

{

if(n==0)

{

cout<<"Case"<

"<<0<

cin>>n>>m;

continue;

}

for(inti=1;i<=n;i++)

{

student[i].father=i;

student[i].count=1;

}

for(intj=0;j

{

inta,b;

cin>>a>>b;

uni(a,b);

}

intsum=0;

vectora;

for(inti=1;i<=n;i++)

{

intre=student[i].father;

if(!

binary_search(a.begin(),a.end(),re))

{

a.push_back(re);

sort(a.begin(),a.end());

}

}

cout<<"Case"<

"<

cin>>n>>m;

}

return0;

}

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 高等教育 > 其它

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1