實驗一、Joseph問題
???
問題描述
:約瑟夫(Joseph)問題的一種描述是:編號為1,2, 。。。,n的n個人按順時針方向圍坐一圈,每人持有一個密碼(正整數)。一開始任選一個正整數作為報數上限值m,從第一個人開始按順時針方向自1開始順序報數,報到m時停止報數。報到m的人出列,將他的密碼作為新的m值,從他在順時針方向上的下一個人開始重新從1報數,如此下去,直到所有人全部出列為止。試設計一個程序求出出列順序。
這是我們數據結構第一個實驗題目。大部分都用C語言寫。我用C++寫的,寫到一半編譯通過了,run的時候出了問題。還沒有寫完,只把用用到的類寫了出來。第一次這樣寫,不知道那里除了問題。
代碼:
??1
#include?
"
iostream.h
"
??2
//
#include?"String.h"
??3
??4
??5
??6
class
?PersonList;
??7
class
?Joseph;
??8
??9
class
?Person
{
?10
//
friend?PersonList;
?11
private
:
?12
???
char
?num;
?13
???
int
?value;
?14
??
//
?Person?*next;
?15
public
:
?16
?Person?
*
next;
?17
?Person();
?18
????Person(
char
,
int
);
?19
????Person(Person??
&
A);
?20
}
;
?21
?22
Person::Person(
char
?A,
int
?Val)
?23
{
?24
????num
=
A;
?25
????value
=
Val;
?26
?27
}
?28
Person::Person(Person?
&
B)
?29
{
?30
????num
=
B.num;
?31
????value
=
B.value;
?32
????next
=
B.next;
?33
}
?34
class
?PersonList
{
?35
friend?Joseph;
?36
public
:
?37
????Person?
*
head,
*
tail;
?38
?
int
?sum;
?39
public
:
?40
????PersonList();
?41
????
~
PersonList();
?42
????
void
?initialPL();
?43
????
void
?insertPl(PersonList?
&
Pl,Person?x);
?44
????
void
?DeleteBlPerson(Person?y);
?45
????
bool
?NEmpty();
?46
}
;
?47
?48
PersonList::PersonList()
?49
{
?50
?Person?
*
head
=
new
?Person;
?51
?Person?
*
tail
=
head;
?52
?53
}
?54
?55
void
?PersonList::insertPl(PersonList?
&
PL,Person?x)
?56
{
?57
????Person?
*
p
=
new
?Person(x);
?58
????PL.tail
->
next
=
p;
?59
????PL.tail
=
p;
?60
????PL.tail
->
next
=
head;
?61
?62
}
?63
void
?PersonList::DeleteBlPerson(Person?y)
?64
{
?65
????Person?p
=*
y.next;
?66
?y.next
=
y.next
->
next;
?67
?68
}
?69
?70
?71
class
?Joseph
{
?72
private
:
?73
?PersonList?List;
?74
?75
public
:
?76
?
char
?
*
JosephList;?? //用來存放輸出序列
?77
?Joseph();
?78
?79
?80
}
;
?81
?82
Joseph::Joseph()
?83
{
?84
?PersonList?P;
?85
?P.initialPL();
?86
?cout
<<
"
人數?:
"
<<
endl;
?87
?cin
>>
P.sum;
?88
?
for
(
int
?i
=
P.sum;i
>
0
;i
--
)
?89
?
{
?90
??cout
<<
"
代號
"
<<
endl;
?91
??
char
?a;
?92
??cin
>>
a;
?93
??cout
<<
"
值
"
<<
endl;
?94
??
int
?x;
?95
??cin
>>
x;
?96
??Person?Z(x,a);
?97
??P.insertPl(P,Z);
?98
?}
?99
100
101
}
102
103
104
105
int
?main()
106
{
107
????Joseph?B;
108
????cout
<<
"
輸入
"
;
109
110
111
????
return
?
0
;
112
}
113
#include?
"
iostream.h
"
??2
//
#include?"String.h"
??3
??4
??5
??6
class
?PersonList;??7
class
?Joseph;??8
??9
class
?Person
{?10
//
friend?PersonList;
?11
private
:?12
???
char
?num;?13
???
int
?value;?14
??
//
?Person?*next;
?15
public
:?16
?Person?
*
next;?17
?Person();?18
????Person(
char
,
int
);?19
????Person(Person??
&
A);?20
}
;?21
?22
Person::Person(
char
?A,
int
?Val)?23
{?24
????num
=
A;?25
????value
=
Val;?26
?27
}
?28
Person::Person(Person?
&
B)?29
{?30
????num
=
B.num;?31
????value
=
B.value;?32
????next
=
B.next;?33
}
?34
class
?PersonList
{?35
friend?Joseph;?36
public
:?37
????Person?
*
head,
*
tail;?38
?
int
?sum;?39
public
:?40
????PersonList();?41
????
~
PersonList();?42
????
void
?initialPL();?43
????
void
?insertPl(PersonList?
&
Pl,Person?x);?44
????
void
?DeleteBlPerson(Person?y);?45
????
bool
?NEmpty();?46
}
;?47
?48
PersonList::PersonList()?49
{?50
?Person?
*
head
=
new
?Person;?51
?Person?
*
tail
=
head;?52
?53
}
?54
?55
void
?PersonList::insertPl(PersonList?
&
PL,Person?x)?56
{?57
????Person?
*
p
=
new
?Person(x);?58
????PL.tail
->
next
=
p;?59
????PL.tail
=
p;?60
????PL.tail
->
next
=
head;?61
?62
}
?63
void
?PersonList::DeleteBlPerson(Person?y)?64
{?65
????Person?p
=*
y.next;?66
?y.next
=
y.next
->
next;?67
?68
}
?69
?70
?71
class
?Joseph
{?72
private
:?73
?PersonList?List;?74
?75
public
:?76
?
char
?
*
JosephList;?? //用來存放輸出序列?77
?Joseph();?78
?79
?80
}
;?81
?82
Joseph::Joseph()?83
{?84
?PersonList?P;?85
?P.initialPL();?86
?cout
<<
"
人數?:
"
<<
endl;?87
?cin
>>
P.sum;?88
?
for
(
int
?i
=
P.sum;i
>
0
;i
--
)?89
?
{?90
??cout
<<
"
代號
"
<<
endl;?91
??
char
?a;?92
??cin
>>
a;?93
??cout
<<
"
值
"
<<
endl;?94
??
int
?x;?95
??cin
>>
x;?96
??Person?Z(x,a);?97
??P.insertPl(P,Z);?98
?}
?99
100
101
}
102
103
104
105
int
?main()106
{107
????Joseph?B;108
????cout
<<
"
輸入
"
;109
110
111
????
return
?
0
;112
}
113
?

