📄 考研论坛 - 软件硕士(mse) - [推荐]数据结构(c++)习题解答(页 1) 简化版本.htm
字号:
i;
//m2记录新的次小者<BR> return
data[m2];<BR>}<BR><BR><BR><BR><BR>※ 修改:linxh于2003-09-05
14:31:36修改本文</TD></TR></TBODY></TABLE><BR>
<TABLE class=tableborder cellSpacing=1 cellPadding=4 width="99%"
align=center>
<TBODY>
<TR>
<TD bgColor=#efefef>
<TABLE cellSpacing=0 cellPadding=0 width="100%">
<TBODY>
<TR>
<TD class=bold>linxh</TD>
<TD align=right>2003-9-5 03:49</TD></TR></TBODY></TABLE></TD></TR>
<TR>
<TD class=smalltxt
bgColor=#f8f8f8><B>Re:[推荐]数据结构(C++)习题解答</B><BR><BR>[size=4]第二章 数组[/size]<BR><BR>作为抽象数据类型数组的类声明。<BR>
#include
<iostream.h>
//在头文件“array.h”中<BR>
#include <stdlib.h><BR>
const int
DefaultSize = 30;<BR>
<BR>template <class Type> class Array
{<BR>
//数组是数据类型相同的n(size)个元素的一个集合, 下标范围从0到n-1。对数组中元素<BR>
//可按下标所指示位置直接访问。<BR>
private:<BR>
Type *elements;
//数组<BR>
int ArraySize;
//元素个数<BR>
public:<BR>
Array ( int Size = DefaultSize );
//构造函数<BR>
Array ( const
Array<Type> & x );
//复制构造函数<BR>
~Array ( ) { delete [ ] elements;
}
//析构函数<BR>
Array<Type> & operator = ( const
Array<Type> & A );
//数组整体赋值 (复制)<BR>
Type&
operator [ ] ( int i );
//按下标访问数组元素<BR>
int Length ( ) const { return ArraySize; }
//取数组长度<BR>
void ReSize ( int sz );
//修改数组长度<BR>
}<BR><BR>
顺序表的类定义<BR>
#include <
iostream.h>
//定义在头文件“seqlist.h”中<BR>#include
<stdlib.h><BR>template <class Type> class SeqList
{
<BR>
private:<BR>
Type
*data;
//顺序表的存放数组<BR>
int MaxSize;
//顺序表的最大可容纳项数<BR>
int
last;
//顺序表当前已存表项的最后位置<BR>
int current;
//顺序表的当前指针(最近处理的表项)<BR>public:<BR>
SeqList ( int MaxSize
);
//构造函数<BR>
~SeqList ( ) { delete [ ] data; }
//析构函数<BR>
int Length ( ) const {
return last+1; }
//计算表长度<BR>
int Find ( Type& x )
const;
//定位函数: 找x在表中位置,置为当前表项<BR>
int IsIn ( Type& x
);
//判断x是否在表中,不置为当前表项<BR>
Type *GetData ( ) { return current == -1?NULL :
data[current]; }
//取当前表项的值<BR>
int Insert ( Type& x );
//插入x在表中当前表项之后,置为当前表项<BR>
int Append ( Type& x
);
//追加x到表尾,置为当前表项<BR>
Type * Remove ( Type& x
);
//删除x,置下一表项为当前表项<BR>
Type * First ( );
//取表中第一个表项的值,置为当前表项<BR>
Type * Next ( ) { return
current < last ? &data[++current] : NULL; }<BR>
//取当前表项的后继表项的值,置为当前表项<BR>
Type * Prior ( ) { return
current > 0 ? &data[--current] : NULL; }
<BR>
//取当前表项的前驱表项的值,置为当前表项<BR>
int IsEmpty ( ) { return
last == -1; }
//判断顺序表空否, 空则返回1; 否则返回0<BR>
int IsFull ( ) {
return last == MaxSize-1; }
//判断顺序表满否, 满则返回1; 否则返回0<BR>
}<BR><BR>2-1
设n个人围坐在一个圆桌周围,现在从第s个人开始报数,数到第m个人,让他出局;然后从出局的下一个人重新开始报数,数到第m个人,再让他出局,……,如此反复直到所有的人全部出局为止。下面要解决的Josephus问题是:对于任意给定的n,
s和m,求出这n个人的出局序列。请以n = 9, s = 1, m =
5为例,人工模拟Josephus的求解过程以求得问题的解。<BR>【解答】<BR>
出局人的顺序为5, 1, 7, 4, 3, 6, 9, 2, 8。<BR><BR>2-2
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -