求解(约瑟夫问题) :12个人围成一圈.从1号开始报号,凡是数到5的人就走出圈子(出局).然后继续报号.试问最后一个出局的人是那一个
此程序是出自一本书上的试题,请大家做答????????????????
此程序是出自一本书上的试题,请大家做答????????????????
解决方案 »
- 用Socket进行通信的时候,是用DataStream好一点还是ObjectStream好一点?
- 寻求学习资料
- 怎么样将报表导入到word?
- java对象 == 问题
- 关于private构造方法的问题
- 如何创建java服务程序?
- 如何用正则表达式实现第一、三和四个字符为数字、第二个为":"?
- 各位大虾,请帮助看一个关于键盘事件的错误?
- 求解:书上一个例子有一条语句不理解(附程序),欢迎讨论、在线等待…………
- 如何在javax.swing.JDesktopPane+javax.swing.JInternalFrame中生成ToolBar?
- 关于jfreechart问题
- 关于java.lang.reflect.Method的问题
public static void out(int m,int n){
boolean[] people=new boolean[m];
int outCnt=0;
int index=0;
int tempCnt=1;
while(outCnt<m){
index=(index+1)%m;
if(!people[index]){
tempCnt=tempCnt+1;
if(tempCnt==n){
people[index]=true;
System.out.println("#"+(index+1)+" out");
outCnt++;
tempCnt=0;
}
}
}
}
我只是转载(省得被高人骂我偷....)
-------------------------------------------------------------
//数三退一 方法2 典型的约瑟夫环问题
public class count3quit2
{
public static void main(String[] args)
{
KidCircle kc = new KidCircle(500);
int countNum = 0;
Kid k = kc.firstKid;
//kc.count;
while(kc.count > 1)
{
countNum++;
if(countNum==3)
{
countNum = 0;
kc.delete(k);
}
k = k.rightKid;
}
System.out.println(kc.firstKid.id);
}
}class Kid
{
int id;
Kid leftKid;
Kid rightKid;
}class KidCircle
{
int count = 0;//圈内的人数,刚开始的圈默认是0
Kid firstKid,lastKid;//开头的小孩和结束的小孩
KidCircle(int n)//构造函数,构造一个指定人数的手拉手的圈子
{
for(int i = 0; i<n; i++)
add();//调用指定次数的add方法
} void add()//向圈内添加小孩的add方法
{
Kid k = new Kid();
k.id = count;//添加的小孩是第几个小孩,添加在哪里?
if(count<=0)//表明圈内一个小孩都没有
{
firstKid = k;
lastKid = k;
k.leftKid = k;
k.rightKid = k;
}
else
{
lastKid.rightKid = k;//最后一个小孩的右边是要加入的当前的小孩
k.leftKid = lastKid;//当前要加入的小孩的左手是最后一个小孩
k.rightKid = firstKid;//当前要加入的小孩的的右手是第一个小孩
firstKid.leftKid = k;//第一个小孩的左手是当前要加入的小孩
lastKid = k;//最后一个小孩成为当前要加入的小孩
}
count++;
} void delete(Kid k)//退出一个小孩的delete方法
{//双向回环链表 约瑟夫环问题
if(count <= 0)
{
return;
}
else if(count == 1)
{
firstKid = lastKid = null;
}
else
{
k.leftKid.rightKid = k.rightKid;//?
//lastKid.rightKid = firstKid.leftKid
k.rightKid.leftKid = k.leftKid;//? if(k == firstKid)
{
firstKid = k.rightKid;
}
else if(k == lastKid)
{
lastKid = k.leftKid;
}
}
count--;
}
}
int x//the x-th person is to be killedreturn ((m-1)*x+1)%m;that's the last left
//m个人,每数到n出局
public static void out(int m,int n){
boolean[] people=new boolean[m];
int outCnt=0;
int index=0;
int tempCnt=1;
while(outCnt<m){
index=(index+1)%m;
if(!people[index]){
tempCnt=tempCnt+1;
if(tempCnt==n){
people[index]=true;
System.out.println("#"+(index+1)+" out");
outCnt++;
tempCnt=0;
}
}
}
做一个解释
int boy_num=12;
newlist nl=new newlist(boy_num);
int way=5;
public run(){
for(int i=0;i<boy_num;i++){
for(int j=0;j<way;j++){
nl.next();
}
nl.gandiao();
}
}
public static void main(String [] ars){
new run();
}
}
class newlist{
int[] boys;
int num;
int location;
public newlist(int a){
location=-1;
num=a;
boys=new int[num];
for(int i=0;i<num;i++)
boys[i]=1;
}
public void next(){
if(location==(boys.length-1)){
location=0;
if(boys[location]==0){
next();
}
}else {location++;
if(boys[location]==0){
next();
}
}
}
public void gandiao(){
boys[location]=0;
System.out.println("我挂了,我市"+(location+1)+"号");
// for(int i=0;i<12;i++)System.out.print(boys[i]+",");
// System.out.println("");
}
}
写的很简单 希望有帮助