天津大学版C++答案.docx
《天津大学版C++答案.docx》由会员分享,可在线阅读,更多相关《天津大学版C++答案.docx(26页珍藏版)》请在冰豆网上搜索。
天津大学版C++答案
练习1
4.
(1)5
(2)2(3)1(4)0(5)1(6)1
(7)9(8)13(9)1(10)2(11)5(12)0
5.
(1)D
(2)D(3)B、C(4)C(5)C(6)C
6.
k=2
234
220
1
(1)
(2)
5
*********6
8.1235
-4.062
(3)
1234.5
1234
(4)
18
第二章
2.阅读下列程序并写出运行结果
(1)
****
(2)
6
(3)
S=11
(4)
14
(5)
执行该程序,当分别输入5238、316、24、0时,输出结果是:
5238
Sum=18
316
sum=10
24
sum=6
0
sum=0
第三章
2.阅读下列程序并写出运行结果
(1)
(2)
(3)
(4)
第四章
2.写出以下程序的运行结果
(1)
(2)
(3)
(4)
(5)
(6)
练习5
2.单选题
(1)A
(2)A(3)D(4)C(5)A(6)A
3.写出以下程序的输出结果。
(1)
(2)
4.程序填空
(1)j(2)longintmyclass:
:
sum=0ob1或ob2或ob3
4.程序填空题
(1)j<=p0line%10==0200ob1.Run()
(2)longintmyclass:
:
sum=0ob1或ob2或ob3
练习6
2.选择题
(1)A
(2)D(3)C(4)A(5)D
3.阅读下列程序并写出输出结果
(1)
(2)
4.程序填空题
(1)person(n,s)person(n,s)
(2)circletablechar[strlen(c)+1]strcpygetheightgetareagetcolor
练习7
2.选择题
(1)D
(2)C(3)A(4)C(5)C
3.阅读下列程序并写出输出结果:
(1)
(2)
4.程序填空题
constpoint&pfriend
练习8
2.选择题
(1)B
(2)B(3)D(4)A(5)B
3.阅读下列程序并写出输出结果:
(1)
(2)
(3)
4.程序填空题
(1)ofstreamoutfile!
outfile或outfile.fall()outfileoutfile.close()
(2)”data.dat”outfile
练习9
1.单选题
(1)A
(2)B(3)D(4)C(5)D(6)B(7)D(8)C
(9)B(10)B(11)A(12)C(13)B(14)D
2.简答题
(6)所有可能的顺序是:
123、132、213、231、321
(9)LOC(aij)=LOC(a00)+(j*m+i)*C
(11)668
0015
0322
05-12
1111
125
23-7
4087
5228
3.程序填空题:
(1)B.ClearList()A.Length()i++
(2)current!
=NULLnexthead->next;
(3)len-i-1A.ClearList();
4.算法设计题
(1)
voidSeqlist:
:
Reverse(){
chartemp;
for(inti=0;itemp=element[i];
element[i]=element[length-1-i];
element[length-1-i]=temp;
}
}
(2)
解法1:
voidSeqList:
:
Delitem(constchar&item){
charx;
for(inti=0;iFind(i,x);
if(x==item){
Delete(i,x);
i--;
}
}
}
解法2:
voidSeqList:
:
Delitem(constchar&item){
inti,j;
for(i=0;iif(element[i]==item){
for(j=i;jelement[j]=element[j+1];
length--;
i--;
}
}
(3)
voidSeqList:
:
Half(){
charx;
for(inti=0;iDelete(i,x);
}
(4)
解法1:
增加一个成员函数Max()
voidSeqlist:
:
Max(int&j,char&max){
max=element[0];
intj=0;
for(inti=1;iif(element[i]>max){
max=element[i];
j=i;
}
}
解法2:
不增加成员函数
#include“seqlist.h”
#include
usingnamespacestd;
voidmain(){
Seqlistlist(100);
chara[10];
cin>>a;
for(inti=0;a[i];i++)
if(list.Insert(i,a[i])==false){
cout<<”插入异常\n”;
break;
}
intp=0;
charx,max,
list.Find(0,max);
for(i=1;i{
list.Find(i,x);
if(x>max){
max=x;
p=i
}
}
cout<<”表中元素最大值为:
”<”<
}
(5)
#include“chain.h”
#include“seqstack.h”
voidreverse(China&A,LinkedStack&B){
B.ClearStack();
charx;
For(inti=0;iA.Find(i,x);
B.Push(x);
}
A.ClearList();
i=0
wihle(!
B.IsEmpty()){
Top(ch);
A.Insert(i,x);
i++;
Pop();
}
B.ClearStack();
}
(6)
voidChain:
:
rInsert(char&x){
Node*p=head,*q;
q=newNode;
q->data=x;
while(p->next!
=NULL){
p=p->next;
}
q->next=p->next;//或q->next=NULL;
p->next=q;
length++;
}
(7)在SeqList类中
voidSeqList:
:
Insertasc(constchar&x){
if(length==Maxsize)return;
for(intk=length-1;k>=0&&element[k]>x;k--)
element[k+1]=element[k];
element[k+1]=x;
length++;
}
在Chain类中
voidChain:
:
Insertasc(constchar&x){
Node*p=head;
while(p->next!
=NULL&&p->next->datap=p->next;
Node*q;
q=newNode;
q->data=x;
q->next=p->next;
p->next=q;
length++;
}
(9)
voidmain(){
charstr[]="C++FORTRAN3PASCAL4basic";
QueueQ1(50),Q2(50);
inti;
for(i=0;str[i]!
=’\0’;i++){
if(str[i]>='A'&&str[i]<='Z')
Q1.EnQueue(str[i]);
elseif(str[i]>='a'&&str[i]<='z')
Q2.EnQueue(str[i]);
}
}
charch;
while(!
Q1.IsEmpty()){
Q1.Front(ch);
cout<Q1.DeQueue();
}
cout<while(!
Q2.IsEmpty()){
Q2.Front(ch);
cout<Q2.DeQueue();
}
cout<}
练习10
1.单选题
(1)C
(2)B(3)C(4)B5)D(6)C(7)B(8)C(9)A(10)B
2.简答题
(1)树:
二叉树:
(2)设树中结点总数为n,叶结点数为n0,则有:
n=n0+n1+n2+...+nm
(1)
设树中的边数为b,则有:
b=n-1和b=n1+2*n2+...+m*nm
于是得:
n=n1+2*n2+...+m*nm+1
(2)
由
(1)、
(2)得:
n0=n2+2*n3+...+(m-1)*nm+1
(4)需增加4个“虚结点”
(5)先序序列:
ABDGCEHIF
中序序列:
DGBAHEICF
后序序列:
GDBHIEFCA
(6)50
(7)对错错对
(8)
(9)右单支树左单支树只有一个结点
(10)
(11)
(a)(b)(c)(d)
(e)
(12)该哈夫曼树共有结点的个数是:
2n-1
(13)对应的哈夫曼树是:
WPL=(16+17)*2+(9+14+15)*3+6*4+(2+3)*5=229
(14)
(1)
顶点
入度
出度
A
0
2
B
1
3
C
3
1
D
2
1
E
2
1
(2)邻接矩阵:
11100
10111
10010
10001
00100
(3)邻接表:
A
B
C
D
E
(15)是强连通图。
它的强连图分量即为图本身。
(16)n个顶点、n-1条边
(17)
(18)
初始时:
s
dist
Path
1
1
0
1
2
0
20
1
3
0
15
1
4
0
∞
0
5
0
∞
0
6
0
∞
0
选择V3后:
s
dist
Path
1
1
0
1
2
0
19
3
3
1
15
1
4
0
∞
0
5
0
∞
0
6
0
25
3
选择V2后:
s
dist
Path
1
1
0
1
2
1
19
3
3
1
15
1
4
0
44
2
5
0
29
2
6
0
25
3
选择V6后:
s
dist
Path
1
1
0
1
2
1
19
3
3
1
15
1
4
0
29
6
5
0
29
2
6
1
25
3
选择V4后:
s
dist
Path
1
1
0
1
2
1
19
3
3
1
15
1
4
1
29
6
5
0
29
2
6
1
25
3
选择V5后:
s
dist
Path
1
1
0
1
2
1
19
3
3
1
15
1
4
1
29
6
5
1
29
2
6
1
25
3
V1到V2的最短路径:
V1V3V2
V1到V3的最短路径:
V1V3
V1到V4的最短路径:
V1V3V6V4
V1到V5的最短路径:
V1V3V2V5
V1到V6的最短路径:
V1V3V6
(19)
各事件的最早发生时间和最迟发生时间:
顶点
eei
lei
V0
0
0
V1
5
9
V2
6
6
V3
12
12
V4
15
16
V5
16
20
V6
17
17
V7
19
20
V8
22
22
V9
24
24
各活动的最早开始时间和最迟开始时间:
活动
ei
li
li-ei
a0
0
4
4
a1
0
0
0
a2
5
9
4
a3
6
6
0
a4
6
13
7
a5
12
13
1
a6
12
16
4
a7
12
12
0
a8
15
16
1
a9
15
16
1
a10
16
20
4
a11
17
17
0
a12
19
20
1
a13
22
22
0
工程的最短完成时间:
24
关键活动:
a1a3a7a11a13
(20)可能的三个拓扑序列:
1,3,7,4,9,5,2,8,6
9,2,1,3,7,5,4,8,6
9,1,3,7,5,2,8,4,6
练习14
1.单项选择题:
(1)C
(2)C(3)B(4)B(5)C(6)C
(7)CABD
2.
(2)52
(3)二叉排序树为:
P0
P2
P6
查找99的过程:
99与根结点P0的值60比较,因99>60,进入P0右子树;99与P2结点的值75比较,因99>75,进入P2右子树;99与P6结点的值99比较,相等,查找成功。
(5)
各关键字的散列地址为:
h(19)=6,h(14)=1,h(23)=10,h(01)=1,h(68)=3,h(20)=7,h(84)=6
h(27)=1,h(55)=3,h(11)=11,h(10)=10,h(79)=1
线性探查开放地址法:
0
1
2
3
4
5
6
7
8
9
10
11
12
14
01
68
27
55
19
20
84
79
23
11
10
链地址法:
0
1
2
3
4
5
6
7
8
9
10
11
12
(6)n(n+1)/2
(10)初始堆:
选择4后再建堆:
选择5后再建堆:
选择9后再建堆:
选择11后再建堆:
选择18后再建堆
选择19后再建堆
选择21后再建堆:
选择29后再建堆:
(11).最坏情况发生在:
文件中的记录在初始时已按排序码值的升序或降序排列。
3.程序填空题
b1b1b1