一,問題描述 500個人圍成一圈 ,從第乙個人開始數數,第乙個人數1 第二個人數2 第三個人數3 ,數到3的人退出圈子,第四個人重新開始數數 ,第四個數1 ,第五個2 ,第六個 3(退出圈子),一直這樣數下去,求最後剩餘的那個人所在位置。
二,問題詳細求解思路
1.面向過程的思想 陣列表示500個人圍成乙個圈 陣列有型別 描述該人不在圈子中 boolean cicle = new boolean[500] true 表示在圈子中 false 表示不在
數數從陣列的下標為0的那個元素開始
count = 0 用來計數
數數的過程 迴圈的過程 退出迴圈的條件是圈子中只剩下乙個人,cicle陣列中只有乙個值為true的元素的時候就退出
leftcont = 500 表示陣列中還剩餘值為true的元素的個數(剩餘人的個數)
index = 0;//記錄陣列的下標
while
(leftcont >1)
if(cicle[index]
) count ++
判斷index ++ 之後是否為3
if(count ==3)
退出圈子
cicle[index]
=false
重新賦值index的值
count =0;
leftcont --
index ++
//i不能超過陣列的長度
if(index >=
500)
index =
0
2.物件導向封裝的思想 人物件 person
id 表示在圈子的位置
左邊的人物件 left
右邊的人物件 right
圈子物件 cicle
圈子大小 count = 0
圈子的第乙個人person first
圈子的最後乙個人person last
方法抽象
圈子要初始化
new cicle(500)
方法 加人(person p)
圈子中已經有人的情況
count ++
原來圈子中最後乙個人的右邊 > 新加人p
新加人p的左邊 》 原來圈子中的最後乙個人
新加人p的右邊 》 原來圈子中第乙個人
原來圈子中最後乙個人 = 新加人p
如果是乙個空圈 count= 0
圈子中的第乙個人 = 新加人p
圈子中最後乙個人 = 新加人p
方法 刪除人(person p)
刪除人p左邊的人 》 刪除人右邊的人
刪除人右邊的人 》 刪除人左邊的人
如果刪除的是第乙個人
刪除人的右邊的人變成了圈子中的第乙個人
如果刪除人是最後乙個人
刪除人的左邊變成了圈子中的最後乙個人
一,**結構
person類: 用於封裝人物件的,主要成員有:1.自身的id 2.右邊的人 3.左邊的人。
personcircle類: 用於封裝圈物件:主要成員有:1.圈內人數 2.圈內第乙個人 3.圈內最後乙個人。
test類: 用於測試程式的類
二,可執行**
person類:
package com.kp;
/*** @author fiee
@filecomment /**
* */
public
class
person
/** * @param id
*/public
person
(int id)
/** * @return the id
*/public
intgetid()
/** * @param id the id to set
*/public
void
setid
(int id)
/** * @return the left
*/public person getleft()
/** * @param left the left to set
*/public
void
setleft
(person left)
/** * @return the right
*/public person getright()
/** * @param right the right to set
*/public
void
setright
(person right)
}
h6>personcircle類:
package com.kp;
/**@author fiee
*/public
class
personcircle
/** * @param count the count to set
*/public
void
setcount
(int count)
/** * @return the first
*/public person getfirst()
/** * @param first the first to set
*/public
void
setfirst
(person first)
/** * @return the last
*/public person getlast()
/** * @param last the last to set
*/public
void
setlast
(person last)
/** * @param count
*/public
personcircle
(int count)
}/**
* * @param p 進圈的人
*/public
void
add(person p)
else
this
.count++;}
/** *
* @param p 出圈的人
*/public
void
remove
(person p)
//如果刪除的是最後乙個人
if(p.
equals
(p.getleft()
))this
.count--;}
public
void
location
(personcircle pc)
p=p.
getright()
;}system.out.
println
("最後在圈裡的人是:"
+pc.first.
getid()
);}/**
* 列印圈裡的id
*/public
void
printall()
}}
test類:
package com.kp;
/**@author fiee
*/public
class
test
}
三,執行結果
012
34本次出去的人的id是:2
本次出去的人的id是:0
本次出去的人的id是:4
本次出去的人的id是:1
最後在圈裡的人是:3
JAVA 數三退一問題的解決
數三退一 500個小孩手拉手圍成一圈,從第乙個小孩開始數數,按照1 2 3 1 2 3迴圈不斷的數,數到3的小孩退出圈,其他小孩接著數,直到剩下乙個小孩,問這個小孩的排在什麼位置?思路分析 可以定義乙個布林型的陣列,用來存放500個小孩,若為true,代表在圈內,若為false,則代表出圈。首先,每...
解決數三退一問題(陣列,物件導向)
500個小孩手拉手圍成乙個圈,第乙個小孩從0開始數,數到3,就淘汰出局,退出這個圈,直至剩餘最後乙個,輸出該小孩。建立小孩陣列,並將每個小孩賦布林初值為 true 表示在圈內 預設從陣列下標為0開始數,用count 計數 初值為1 當count 3 淘汰這個小孩,即將其值設為false,並將計數器c...
繼續磕演算法呵呵。數三退一問題。
邏輯倒是夠清晰,不過直覺告訴我方法肯定能改進。初始化陣列。boolean list new boolean 500 for int i 0 i list.length i count用來數三,limit用來在陣列裡只有乙個true的時候結束迴圈。int count 0 int limit 0 如果上...