编写一个小算法

zly361814478 2012-09-25 02:32:12
有500个小朋友编号1-500 围成一圈,做1-3数数小游戏。 数到3的时候 那位小朋友推出,一直循环,算出最后一个小朋友的编号。
...全文
242 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
zly361814478 2012-09-26
  • 打赏
  • 举报
回复
嗯,我已经自己实现了。谢谢各位
Mourinho 2012-09-26
  • 打赏
  • 举报
回复
用队列做
把报到1,2的人从队列前端移除并添加到原队列末尾,进行下一次报数。
报到3的人直接从队列中移除。
直到队列长度为1


C#代码,java的类似

static int GetNum(int n)
{
Queue<int> queue = new Queue<int>();
for (int i = 1; i <= n; ++i) queue.Enqueue(i);
int num = 1;
while (queue.Count > 1)
{
if (num++ % 3 == 0) queue.Dequeue();
else queue.Enqueue(queue.Dequeue());
}
return queue.Dequeue();
}

_jerrytiger 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]

public class Test{
public static void main(String[] args){
//创建1-500个小朋友编号
int stuNumber=500;
int[] childs=new int [stuNumber];
for(int i=0;i<childs.length;i++){
childs[i]=i+1;
}
//统计数到三
i……
[/Quote]

和我6楼的结果一样,对的。
wwwcomcn123 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 的回复:]

按你的要求写的,记得给我点分哈。[/Quote]
貌似楼主结贴率为0.呵呵
toBetterWu 2012-09-25
  • 打赏
  • 举报
回复
public class Test{
public static void main(String[] args){
//创建1-500个小朋友编号
int stuNumber=500;
int[] childs=new int [stuNumber];
for(int i=0;i<childs.length;i++){
childs[i]=i+1;
}
//统计数到三
int iNum=0;
//统计结束标记
int stopNumber=0;
while(true){
stopNumber=0;
for(int i=0;i<childs.length;i++){
if(childs[i]!=-1){
iNum++;
if(iNum==3){
//判断数到了3的小朋友编号都设置为-1
childs[i]=-1;
iNum=0;
}
}else{
stopNumber++;
}

}

//当这些小朋友里面只有一个不是-1的时候结束
if(stopNumber==childs.length-1){
for(int i=0;i<childs.length;i++){
if(childs[i]!=-1){
System.out.println("这个学生的号码是"+childs[i]);
}
}
break;
}

}
}
}

结果是 436哦
LogicTeamLeader 2012-09-25
  • 打赏
  • 举报
回复
不需要那么复杂,用原始数组即可,不用链表插来删去的。

public class Kids {

private static final int LENGTH = 500;

public static void main(String[] args) {
int[] array = new int[LENGTH];
for (int i = 0; i < array.length; i++) {
array[i] = i + 1;
}

int n = 0;
int i = 0;
while (getSize(array) > 1) {
if (array[i] > 0) {
n++;
}
if ((n % 3) == 0) {
array[i] = -1;
}
if (i < LENGTH - 1) {
i++;
} else {
i = 0;
}
}

for (int k : array) {
if (k > 0) {
System.out.println("the number of the last kid is:" + i);
}
}
}

public static int getSize(int[] array) {
int n = 0;
for (int i : array) {
if (i > 0) {
n++;
}
}
return n;
}
}



结果是268.
zhaoming262350 2012-09-25
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 的回复:]

Java code

package com.zf.test;

import java.util.LinkedList;
import java.util.List;

public class Test3 {

//虚拟 n 个children
public List<Integer> buildChildren(int childrenSize){
……
[/Quote]这个方法可以
程序员的崛起 2012-09-25
  • 打赏
  • 举报
回复
面向对象的:
package com.test;

public class CountQuit {
public static void main(String args[]) {
KidCircle kc = new KidCircle(500);
int countNum = 0;
Kid k = kc.first;
while(kc.count > 1) {
countNum ++ ;
if(countNum == 3) {
countNum = 0;
kc.delete(k);
}
k = k.right;
}
System.out.println(kc.first.id);
}
}

class Kid {
int id;
Kid left;
Kid right;
}

class KidCircle {
int count = 0;
Kid first, last;
KidCircle(int n) {
for(int i=0; i<n; i++) {
add();
}
}

void add() {
Kid k = new Kid();
k.id = count;
if(count <= 0) {
first = k;
last = k;
k.left = k;
k.right = k;
} else {
last.right = k;
k.left = last;
k.right = first;
first.left = k;
last = k;
}
count ++;
}

void delete(Kid k) {
if(count <=0) return ;
else if(count == 1) {
first = last = null;
} else {
k.left.right = k.right;
k.right.left = k.left;
if(k == first) {
first = k.right;
} else if(k == last) {
last = k.left;
}
}
count --;
}
}



_jerrytiger 2012-09-25
  • 打赏
  • 举报
回复

package com.zf.test;

import java.util.LinkedList;
import java.util.List;

public class Test3 {

//虚拟 n 个children
public List<Integer> buildChildren(int childrenSize){
List<Integer> children = new LinkedList<Integer>();
for (int i = 1 ; i <= childrenSize; i++) {
children.add(i);
}
return children;
}


//递归计算
public int calc (int currentNo , int currentListIndex , List<Integer> children){
if(currentListIndex == children.size()){
if(children.size() > 1){
currentListIndex = 0 ;
}else{
return children.get(0);
}
}

if(currentNo == 3){
children.remove(currentListIndex);
currentNo = 1;
}else{
currentNo++;
currentListIndex++;
}
return calc(currentNo , currentListIndex , children) ;
}

//包装
public int run( int childrenSize){
return calc(1 , 0 , buildChildren(childrenSize));
}

public static void main(String[] args) {

Test3 t = new Test3();
System.out.println("最后退出的小朋友为:" + t.run(500));

}

}

baiyu110 2012-09-25
  • 打赏
  • 举报
回复
呵呵.. 楼主试试我的...

import java.util.Arrays;


public class TestString {
public static void main(String[] args) {
boolean[] c = new boolean[500];
Arrays.fill(c, true);
int count = 0;
int num = 0;
for (int i=0; true; i++) {
if (!c[i % c.length]) {
i++;
continue;
}
if (++count % 3 == 0) {
c[i % c.length] = false;
num++;
}
if (num == 499) {
break;
}
}

for (int i = 0; i < c.length; i++) {
if (c[i]) {
System.out.println("最后一个小朋友的编号是" + (i + 1) );
}
}
}
}
zly361814478 2012-09-25
  • 打赏
  • 举报
回复
这是一个圆圈,应该循环到最后还剩两个人啊[Quote=引用 3 楼 的回复:]
你这个不是就循环了一次么?

引用 2 楼 的回复:
按你的要求写的,记得给我点分哈。
public class test2
{
static {System.out.println("aaaaaaaaaa");}
public static void main(String[] args)
{
//创建1-500个小朋友编号
int[] childs=new int [500……
[/Quote]
zly361814478 2012-09-25
  • 打赏
  • 举报
回复
你这个不是就循环了一次么?[Quote=引用 2 楼 的回复:]
按你的要求写的,记得给我点分哈。
public class test2
{
static {System.out.println("aaaaaaaaaa");}
public static void main(String[] args)
{
//创建1-500个小朋友编号
int[] childs=new int [500];
for(int i=0;i<500;i++){
……
[/Quote]
  • 打赏
  • 举报
回复
按你的要求写的,记得给我点分哈。
public class test2
{
static {System.out.println("aaaaaaaaaa");}
public static void main(String[] args)
{
//创建1-500个小朋友编号
int[] childs=new int [500];
for(int i=0;i<500;i++){
childs[i]=i+1;
}
//判断数到了3的小朋友编号
int iNum=0;
for(int i=0;i<500;i++){
iNum++;
if(iNum==3){
System.out.println("数到3的小朋友编号是"+childs[i]);
iNum=0;
}
}

}
}
nmyangym 2012-09-25
  • 打赏
  • 举报
回复
约瑟夫环问题:

http://topic.csdn.net/u/20120627/20/4f300b35-b616-4d3d-8a22-e5313f9fc3cb.html

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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