VERSION 5.00 Begin VB.Form fan3 Caption = "约瑟夫问题——反三" ClientHeight = 6015 ClientLeft = 60 ClientTop = 345 ClientWidth = 6795 LinkTopic = "Form1" ScaleHeight = 6015 ScaleWidth = 6795 StartUpPosition = 3 '窗口缺省 Begin VB.Timer Timer1 Left = 480 Top = 5400 End Begin VB.CommandButton Command1 BackColor = &H00C0C0C0& Caption = "Command1" Height = 615 Index = 0 Left = 720 Style = 1 'Graphical TabIndex = 17 Top = 840 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 1 Left = 1800 Style = 1 'Graphical TabIndex = 16 Top = 840 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 2 Left = 2880 Style = 1 'Graphical TabIndex = 15 Top = 840 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 3 Left = 4080 Style = 1 'Graphical TabIndex = 14 Top = 840 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 4 Left = 4080 Style = 1 'Graphical TabIndex = 13 Top = 1590 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 5 Left = 4080 Style = 1 'Graphical TabIndex = 12 Top = 2325 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 6 Left = 4080 Style = 1 'Graphical TabIndex = 11 Top = 3075 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 7 Left = 4080 Style = 1 'Graphical TabIndex = 10 Top = 3810 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 8 Left = 4080 Style = 1 'Graphical TabIndex = 9 Top = 4560 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 10 Left = 2400 Style = 1 'Graphical TabIndex = 8 Top = 4560 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 11 Left = 1560 Style = 1 'Graphical TabIndex = 7 Top = 4560 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 13 Left = 720 Style = 1 'Graphical TabIndex = 6 Top = 3810 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 14 Left = 720 Style = 1 'Graphical TabIndex = 5 Top = 3075 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 15 Left = 720 Style = 1 'Graphical TabIndex = 4 Top = 2325 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 16 Left = 720 Style = 1 'Graphical TabIndex = 3 Top = 1590 Width = 735 End Begin VB.CommandButton Command2 Caption = "Command2" Height = 855 Left = 2400 TabIndex = 2 Top = 2520 Width = 855 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 12 Left = 720 Style = 1 'Graphical TabIndex = 1 Top = 4560 Width = 735 End Begin VB.CommandButton Command1 Caption = "Command1" Height = 615 Index = 9 Left = 3240 Style = 1 'Graphical TabIndex = 0 Top = 4560 Width = 735 End End Attribute VB_Name = "fan3" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Option ExplicitPrivate Sub Command2_Click()If Command2.Caption = "开始" Then Timer1.Interval = 300 Command2.Enabled = False Else Me.Hide End If End SubPrivate Sub Form_Load() Dim i As Integer For i = 0 To 16 Command1(i).FontName = "Arial" Command1(i).FontSize = 20 Command1(i).FontBold = True Command1(i).Caption = i Command1(i).Tag = 0 '用于反三 Next i Command2.Caption = "开始"End SubPrivate Sub Timer1_Timer() '反三 Static Count As Integer '个人所报的报数(1-3) Static ID As Integer '圈中人的编号(0-17) Static Total As Integer '退出圈外的人数 Dim i As Integer Static OldID As Integer '上次报数的人的编号Command1(OldID).BackColor = &H8000000F If Total = 16 Then For i = 0 To 16 If Command1(i).Tag = 0 Then Command1(i).BackColor = vbRed Next i Timer1.Interval = 0 Command2.Enabled = True Command2.Caption = "退出" End IfIf Command1(ID).Tag = 0 Then Count = Count + 1 Command1(ID).BackColor = vbYellow OldID = ID
If Count Mod 3 = 0 Then Command1(ID).Tag = 1 Total = Total + 1 Command1(ID).Move Command1(3).Left + (fan3.Width - Command1(3).Left - Command1(3).Width) / 2 + Command1(3).Width, fan3.Height - (Total + 1) * fan3.Height / 17 Command1(ID).FontSize = 12 Command1(ID).Height = fan3.Height / 16 - 20 End If End IfDo ID = (ID + 1) Mod 17 Loop Until Command1(ID).Tag = 0 Or Total = 16End Sub
不是很简单的?如果用VB数组来模拟指针也是十分可行的啊 private type tpeArray iPre as integer sData as string iNext as integer end type dim ary(100) as tpeArray 然后将除了最后一个元素的iNext属性设置成后面一个元素的下标 for i=1 to 99 ary(i).iNext=i+1 ary(i).iPre=i-1 next ary(0).iNext=2 ary(0).iPre=100 ary(100).iNext=0 ary(100).iPre=99 ary(100).iNext=0 这样就形成一个环了 把谁踢出,就是将这个数据删除,也就是把前一个元素的iNext属性设置为被删除元素iNext属性 如果是第k号元素被删除: ary(ary(k).iPre).iNext=ary(k).iNext ary(ary(k).iNext).iPre=ary(k).iPre 进行遍历的时候不能用 for i=0 to 100 了,而是要要用ary(i).iNext来进行这个方法比较复杂,不用这个也是能够完成的。
Private Sub Command1_Click()'用动态数组也是可行的 Dim ary() As Integer Dim iAry As Integer Dim k As Integer Dim iCount As Integer Dim iLoop As Integer Dim iIndex As Integer iAry = 20 ReDim ary(iAry) For j = 0 To 20 ary(j) = j Next k = 5While Not iAry <= 10 iIndex = iIndex + 1 iCount = iCount + 1 If iCount = 5 Then
iAry = iAry - 1 iCount = 0 '删除元素 For iLoop = iIndex To iAry ary(iLoop) = ary(iLoop + 1) Next ReDim Preserve ary(iAry) End If If iIndex > iAry Then iIndex = 0
'debug Open "C:\" & iAry & ".txt" For Output As #1
For j = 0 To iAry Print #1, ary(j) Next Close Wend End Sub
Private Sub Command1_Click() Dim a() As Integer n = 50: k = 5 ReDim a(1 To n) As Integer For i = 1 To n a(i) = i + 1 Next i a(n) = 1: p = 50 For s = 1 To n / 2 For j = 1 To k p1 = p: p = a(p) Next j a(p1) = a(p) Debug.Print p; a(p) = 0: p = p1 Next s End Sub 结果: 5 10 15 20 25 30 35 40 45 50 6 12 18 24 31 37 43 49 7 14 22 29 38 46 3
看看先
#include <iostream.h>
#include <fstream.h>
void main()
{
int n,cs,s=0,i,j;//n:人数;cs:密码;s第几个人出列;i,j:循环用变量
ifstream input("myindata.txt");
input>>n>>cs>>s;
cout<<"人数:"<<n<<" 出始密码"<<cs<<" 初始是第几个人:"<<s<<endl;
int* sz=new int[n+1];
int* sz1=new int[n+1];
for(i=1;i<=n;i++)
{
input>>sz[i];
sz1[i]=i;
cout<<sz[i]<<" ";
}
cout<<endl;
for(i=n;i>=1;i--)
{
s=(s+cs-1)%i;
if(s==0) s=i;
cs=sz[s];
cout<<"第"<<sz1[s]<<"个人出列"<<"密码是:"<<cs<<endl;
for(j=s;j<i;j++)
{
sz[j]=sz[j+1];
sz1[j]=sz1[j+1];
}
}
delete sz;
cin>>i;
}
有n个人围成圈站着,从第一个人开始报数,每数到第k个人,就把他踢出去,接着再往下数到第k个人,再把他踢出去,如此循环,当踢到只剩一半人的时候游戏结束#include<stdio.h>
#define new(type) (type *)malloc(sizeof(type))
typedef struct rNode * RNodePtr;
typedef struct rNode {
int pos;
RNodePtr next;
}RNode,* RingList;
void InitRing(int n,RingList *R)
{ RNodePtr p,q;
int i;
*R=q=new(RNode); //创建有n个结点的循环单链表
for(i=1;i<n;i++)
{ p=new(RNode);
q->pos=i;
q->next=p;
q=p;
}
p->pos=n;
p->next=*R;
*R=p;//R指向循环链表表尾结点,作计数准备
}
void DeleteDeath(int n,int k,RingList *R)
{ int i,j;
RNodePtr p,q;
p=*R;
for (i=1;i<=n/2;i++) //删除一半结点
{ for(j=1;j<=k-1;j++) //沿链前进k-1步
p=p->next;
q=p->next; //q为被删结点
p->next=q->next; //删除
printf("%4d",q->pos); //输出被删除的结点的位置
free(q);
}
*R=p;
printf("\nfinished.\n");
}
main()
{ RingList R;
RNodePtr p,q;
int n,k;
printf("n,k=");
scanf("%d%d",&n,&k);
InitRing(n,&R);
DeleteDeath(n,k,&R);
}
Begin VB.Form fan3
Caption = "约瑟夫问题——反三"
ClientHeight = 6015
ClientLeft = 60
ClientTop = 345
ClientWidth = 6795
LinkTopic = "Form1"
ScaleHeight = 6015
ScaleWidth = 6795
StartUpPosition = 3 '窗口缺省
Begin VB.Timer Timer1
Left = 480
Top = 5400
End
Begin VB.CommandButton Command1
BackColor = &H00C0C0C0&
Caption = "Command1"
Height = 615
Index = 0
Left = 720
Style = 1 'Graphical
TabIndex = 17
Top = 840
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 1
Left = 1800
Style = 1 'Graphical
TabIndex = 16
Top = 840
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 2
Left = 2880
Style = 1 'Graphical
TabIndex = 15
Top = 840
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 3
Left = 4080
Style = 1 'Graphical
TabIndex = 14
Top = 840
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 4
Left = 4080
Style = 1 'Graphical
TabIndex = 13
Top = 1590
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 5
Left = 4080
Style = 1 'Graphical
TabIndex = 12
Top = 2325
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 6
Left = 4080
Style = 1 'Graphical
TabIndex = 11
Top = 3075
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 7
Left = 4080
Style = 1 'Graphical
TabIndex = 10
Top = 3810
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 8
Left = 4080
Style = 1 'Graphical
TabIndex = 9
Top = 4560
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 10
Left = 2400
Style = 1 'Graphical
TabIndex = 8
Top = 4560
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 11
Left = 1560
Style = 1 'Graphical
TabIndex = 7
Top = 4560
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 13
Left = 720
Style = 1 'Graphical
TabIndex = 6
Top = 3810
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 14
Left = 720
Style = 1 'Graphical
TabIndex = 5
Top = 3075
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 15
Left = 720
Style = 1 'Graphical
TabIndex = 4
Top = 2325
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 16
Left = 720
Style = 1 'Graphical
TabIndex = 3
Top = 1590
Width = 735
End
Begin VB.CommandButton Command2
Caption = "Command2"
Height = 855
Left = 2400
TabIndex = 2
Top = 2520
Width = 855
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 12
Left = 720
Style = 1 'Graphical
TabIndex = 1
Top = 4560
Width = 735
End
Begin VB.CommandButton Command1
Caption = "Command1"
Height = 615
Index = 9
Left = 3240
Style = 1 'Graphical
TabIndex = 0
Top = 4560
Width = 735
End
End
Attribute VB_Name = "fan3"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option ExplicitPrivate Sub Command2_Click()If Command2.Caption = "开始" Then
Timer1.Interval = 300
Command2.Enabled = False
Else
Me.Hide
End If
End SubPrivate Sub Form_Load()
Dim i As Integer
For i = 0 To 16
Command1(i).FontName = "Arial"
Command1(i).FontSize = 20
Command1(i).FontBold = True
Command1(i).Caption = i
Command1(i).Tag = 0 '用于反三
Next i
Command2.Caption = "开始"End SubPrivate Sub Timer1_Timer()
'反三
Static Count As Integer '个人所报的报数(1-3)
Static ID As Integer '圈中人的编号(0-17)
Static Total As Integer '退出圈外的人数
Dim i As Integer
Static OldID As Integer '上次报数的人的编号Command1(OldID).BackColor = &H8000000F
If Total = 16 Then
For i = 0 To 16
If Command1(i).Tag = 0 Then Command1(i).BackColor = vbRed
Next i
Timer1.Interval = 0
Command2.Enabled = True
Command2.Caption = "退出"
End IfIf Command1(ID).Tag = 0 Then
Count = Count + 1
Command1(ID).BackColor = vbYellow
OldID = ID
If Count Mod 3 = 0 Then
Command1(ID).Tag = 1 Total = Total + 1
Command1(ID).Move Command1(3).Left + (fan3.Width - Command1(3).Left - Command1(3).Width) / 2 + Command1(3).Width, fan3.Height - (Total + 1) * fan3.Height / 17
Command1(ID).FontSize = 12
Command1(ID).Height = fan3.Height / 16 - 20
End If
End IfDo
ID = (ID + 1) Mod 17
Loop Until Command1(ID).Tag = 0 Or Total = 16End Sub
private type tpeArray
iPre as integer
sData as string
iNext as integer
end type
dim ary(100) as tpeArray
然后将除了最后一个元素的iNext属性设置成后面一个元素的下标
for i=1 to 99
ary(i).iNext=i+1
ary(i).iPre=i-1
next
ary(0).iNext=2
ary(0).iPre=100
ary(100).iNext=0
ary(100).iPre=99
ary(100).iNext=0
这样就形成一个环了
把谁踢出,就是将这个数据删除,也就是把前一个元素的iNext属性设置为被删除元素iNext属性
如果是第k号元素被删除:
ary(ary(k).iPre).iNext=ary(k).iNext
ary(ary(k).iNext).iPre=ary(k).iPre
进行遍历的时候不能用
for i=0 to 100
了,而是要要用ary(i).iNext来进行这个方法比较复杂,不用这个也是能够完成的。
Private Sub Command1_Click()'用动态数组也是可行的
Dim ary() As Integer
Dim iAry As Integer
Dim k As Integer
Dim iCount As Integer
Dim iLoop As Integer
Dim iIndex As Integer
iAry = 20
ReDim ary(iAry)
For j = 0 To 20
ary(j) = j
Next
k = 5While Not iAry <= 10
iIndex = iIndex + 1
iCount = iCount + 1
If iCount = 5 Then
iAry = iAry - 1
iCount = 0
'删除元素
For iLoop = iIndex To iAry
ary(iLoop) = ary(iLoop + 1)
Next
ReDim Preserve ary(iAry)
End If
If iIndex > iAry Then iIndex = 0
'debug
Open "C:\" & iAry & ".txt" For Output As #1
For j = 0 To iAry
Print #1, ary(j)
Next
Close
Wend
End Sub
Dim a() As Integer
n = 50: k = 5
ReDim a(1 To n) As Integer
For i = 1 To n
a(i) = i + 1
Next i
a(n) = 1: p = 50
For s = 1 To n / 2
For j = 1 To k
p1 = p: p = a(p)
Next j
a(p1) = a(p)
Debug.Print p;
a(p) = 0: p = p1
Next s
End Sub
结果: 5 10 15 20 25 30 35 40 45 50 6 12 18 24 31 37 43 49 7 14 22 29 38 46 3