~~~~~~~~~~~~经典算法题,50分求答案

zuoxingyu 2006-03-05 07:49:56
一圈人围坐在一起。一个人开始从1喊数,第二个人喊2,每个人喊的数字都比前一个人多1,当喊叫的数字

是7的倍数或包含7的话,就将该人清除出去,并反向循环喊数。请编写一个函数,计算出给定人数后,那
个人会坚持到最后。


需要用VB写一函数

3KS
...全文
293 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
northwolves 2006-03-06
  • 打赏
  • 举报
回复
当喊叫的数字

是7的倍数或包含7的话,就将该人清除出去,并反向循环喊数
---------------------------
不严谨.意思是只喊1->7->1->7...?
反向循环喊数 什么意思?
KiteGirl 2006-03-06
  • 打赏
  • 举报
回复
程序没问题,只是不知道我数的方式是否和你要求的一样。区别在于删除第7个之后,从哪个元素向回开始数。目前采用的是“后跳”原则:从第1数到第7个;然后删除第7个;这时候第8个成为第7个;从第7个(也就是原来的第8个)向回数……
有四种不同的处理原则:1、后跳;2、前跳;3、方向决定后跳或者前跳;4、原位跳

Celeron 433上,编译后计算1000000次时间是1.36秒。

测试窗体frmMain.frm

Private priPack As tpLoopDelete_Pack
Private priCount As Long
Private priBound As Long

Private Sub Form_Load()
priCount = 1000000 '数量
priBound = 7 '间隔
priPack = LoopDelete_PackCearte(priCount) '初始化链表
End Sub

Private Sub Command1_Click()
Dim tMode As Boolean '方向
Dim tIndexA As Long
Dim tIndexB As Long
Dim tBound As Long

tBound = priBound - 1

For tIndexA = 0 To priCount
tMode = (tIndexA Mod 2)
For tIndexB = 0 To tBound
If tMode Then
LoopDelete_PackIndexNext priPack
Else
LoopDelete_PackIndexBack priPack
End If
Next
LoopDelete_DeleteCell priPack.ldpCells(), priPack.ldpIndex
LoopDelete_PackIndexNext priPack
Next
Text1.Text = priPack.ldpCells(priPack.ldpIndex).ldcValue
End Sub

modLoopDelete.bas文件

Type tpLoopDelete_Cell '元素
ldcValue As Long '值
ldcIndex_Sur As Long '来源指针
ldcIndex_Des As Long '目的指针
End Type

Type tpLoopDelete_Pack
ldpIndex As Long '指针
ldpCells() As tpLoopDelete_Cell '元素数组
End Type

Public Sub LoopDelete_PackIndexBack(ByRef pPack As tpLoopDelete_Pack)
'前跳
pPack.ldpIndex = pPack.ldpCells(pPack.ldpIndex).ldcIndex_Sur
End Sub

Public Sub LoopDelete_PackIndexNext(ByRef pPack As tpLoopDelete_Pack)
'后跳
pPack.ldpIndex = pPack.ldpCells(pPack.ldpIndex).ldcIndex_Des
End Sub

Public Function LoopDelete_PackCearte(ByVal pCount As Long) As tpLoopDelete_Pack
'创建数据包
Dim tOutPack As tpLoopDelete_Pack

With tOutPack
.ldpCells() = LoopDelete_CellsCearte(pCount)
.ldpIndex = 0
End With

LoopDelete_PackCearte = tOutPack
End Function

Public Sub LoopDelete_DeleteCell(ByRef pCells() As tpLoopDelete_Cell, ByVal pIndex As Long)
'删除元素
Dim tCellDesIndex As Long
Dim tCellSurIndex As Long

With pCells(pIndex)
tCellDesIndex = .ldcIndex_Des
tCellSurIndex = .ldcIndex_Sur
End With

With pCells(tCellSurIndex)
.ldcIndex_Des = tCellDesIndex
End With

With pCells(tCellDesIndex)
.ldcIndex_Sur = tCellSurIndex
End With

End Sub

Public Function LoopDelete_CellsCearte(ByVal pCount As Long) As tpLoopDelete_Cell()
'元素数组创建
Dim tOutCells() As tpLoopDelete_Cell

Dim tCells_Index As Long
Dim tCells_Length As Long

tCells_Length = pCount - 1

ReDim tOutCells(tCells_Length)

Dim tCells_Index_LoopStart As Long
Dim tCells_Index_LoopEnd As Long

tCells_Index_LoopStart = 1
tCells_Index_LoopEnd = tCells_Length - 1

With tOutCells(0)
.ldcIndex_Sur = tCells_Length
.ldcIndex_Des = 1
.ldcValue = 0
End With

With tOutCells(tCells_Length)
.ldcIndex_Sur = tCells_Length - 1
.ldcIndex_Des = 0
.ldcValue = tCells_Length
End With

For tCells_Index = tCells_Index_LoopStart To tCells_Index_LoopEnd
With tOutCells(tCells_Index)
.ldcIndex_Sur = tCells_Index - 1
.ldcIndex_Des = tCells_Index + 1
.ldcValue = tCells_Index
End With
Next

LoopDelete_CellsCearte = tOutCells()
End Function
KiteGirl 2006-03-06
  • 打赏
  • 举报
回复
“抓羊羔算法”解决的不是这个问题。

“抓羊羔算法”解决的是:
从一组数字里一个一个地随机提取元素,每提取一个元素则将该元素从数组删除掉。通常的算法要将后面的元素数据拷贝过来填补去掉的元素,或者采用链表实现。“抓羊羔算法”只缩短数组的长度,不仅简单而且速度比较快。

这个拍七的算法利用双向循环链表比较容易解决。
lsftest 2006-03-06
  • 打赏
  • 举报
回复
northwolves(狼行天下) ( ) 信誉:125 2006-03-06 00:54:00 得分: 0
当喊叫的数字
是7的倍数或包含7的话,就将该人清除出去,并反向循环喊数
---------------------------
不严谨.意思是只喊1->7->1->7...?
反向循环喊数 什么意思?
==================================================
老大,你喝不喝酒的????
楼主的意思类似于一个在酒桌上常见的酒令“拍七”的游戏规则。。。。
我记得以前好像看过小仙妹写过一个好像叫什么什么抓羊羔算法的好像是这一类东西。。。
明天找找。。。。。。。
fj182 2006-03-05
  • 打赏
  • 举报
回复
Josephus问题的变种。
zuoxingyu 2006-03-05
  • 打赏
  • 举报
回复
不是作业.朋友给的题目

会的帮忙解答一下,50分送上
faysky2 2006-03-05
  • 打赏
  • 举报
回复
作业?

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧