Topic: 一个csdn上面的一道小题

  Print this page

1.一个csdn上面的一道小题 Copy to clipboard
Posted by: stream
Posted on: 2007-10-19 12:34

从1数到9,数到5就把5去掉,接着数,再数到5就在去掉,最后只剩下一个数,那
个数是什么?

2.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: wmfsczx
Posted on: 2008-01-27 11:56

{1,2,3,4}

3.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: wmfsczx
Posted on: 2008-01-27 12:08

{1,2,3,4}

4.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: lupingui
Posted on: 2008-03-27 18:32

它应该是一个圈吧,从一个地方开始数,一直去,这以前好像是一个什么游戏.一圈人围着,从某个人开始数,数到几就退出,然后再往下,一直到最后一个为止.
1~9最后留下的是4.但如果用程序写我不知道怎么写~ -_-

5.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: istimeto
Posted on: 2008-06-17 00:17

答案应该是:8
public class game
{
int num[];
int length;
int steplength;
int currentnum;
game(int n[],int len,int steplen)
{num=n;
length=len;
steplength=steplen;
currentnum=0;
}

public void del(int curnum)
{
int des;
if(curnum+steplength<=length)
{
des=curnum+steplength-1;
System.out.println("des:");
System.out.println(des);
}
else
{
des=(steplength-(length-curnum)-1)%(length);
}

for(int i=des+1;i<length;i++)
{
num[i-1]=num[i];
}
length--;
currentnum=des%length;

}
void print()
{
for(int i=0;i<length;i++)
System.out.println(num[i]);
System.out.println(",");
}

public static void main(String s[])
{
int nn[]={1,2,3,4,5,6,7,8,9};
int step=5;
game g=new game(nn,nn.length,step);
while(g.length>1)
{

g.del(g.currentnum);
  
g.print();
}
System.out.println(g.num[0]);
}

}

6.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: istimeto
Posted on: 2008-06-17 00:19

编的比较乱Smile
可以用访问者模式较好的呈现。。。。

7.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: climby
Posted on: 2008-06-18 16:51

答案的却是4
我的程序如下:
===============================================

package test.t20080618;

public class Position {

/**
* @param args
*/
public static void main(String[] args) {
int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
int rem = 5;

int lastNum = getBestPosition(nums, rem);
System.out.println(lastNum);
}

public static int getBestPosition(int[] allPositions, int removePosition) {

int index;
while (true) {
if (allPositions.length == 1) {
break;
}
// get the position of the element which should be removed
if (allPositions.length > removePosition) {
index = removePosition;
} else {
index = removePosition;
while (index > allPositions.length) {
index = index - allPositions.length;
}
}
// create a temp array to remove element
int[] temp = new int[allPositions.length - 1];
System.arraycopy(allPositions, 0, temp, 0, temp.length);

for (int i = index; i < allPositions.length; i++) {
temp[i - 1] = allPositions[i];
}
allPositions = temp;
// print the content of the array.
for (int j = 0; j < allPositions.length; j++) {
System.out.print(allPositions[j]);
}
System.out.println();
}
return allPositions[0];
}

}

============================================

运行结果如下:

************************************
12346789
1234789
123489
12349
1234
234
24
4
4
****************************************

8.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: andy_wang_5
Posted on: 2008-06-19 10:34


9.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: billgacsli
Posted on: 2008-06-20 11:07

神呐!!
每次一个数出局后是继续数下去,而不是从头再开始数
4的结果显然不对嘛

10.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: billgacsli
Posted on: 2008-06-20 13:23

我也分享下我的算法。

法一:通过数组实现,伪删除

  /**
   * N个人围成一圈依次从1报数,报到rem的人出局,下一个人重新从1开始报数,直至剩下一个人。
   *
   * @param N 总人数
   * @param rem 数到rem的人出局(注:从1开始数)
   * @param printable 标示是否打印中间过程
   * @return
   */
  public static int getBestPosition2(int N, int rem, boolean printable){
    int positions[] = new int[N];
    for(int i = 0; i < N; i++){
      positions[i] = i + 1;
    }
    int bestNumber = 1;
    int count = 0;
    while(true){
      for(int i = 0; i < N; i++){
        if(positions[i] != 0){//跳过标记为0的数
          count++;
        }
        if(count == rem){
          positions[i] = 0; //出局的数设定为0作标记
          count = 0;
          if(printable){
            print(i, positions);
          }
        }
      }
      if(count == 1){
        for (int i = 0; i < N; i++){ // 从结果中取出最好的数
          if(positions[i] != 0){
            bestNumber = positions[i];
          }
        }
        break;
      }
    }
    return bestNumber;
  }


法二:通过List实现,真删除(ps:这个费了好多劲,下标计算老是不对)

  public static int getBestPosition(int N, int rem, boolean printable){
    List<Integer> positions = new ArrayList<Integer>();
    for(int i = 0; i < N; i++){
      positions.add(i+1);
    }

    int toRemove = (rem - 1) % N; //下标从0开始
    positions.remove(toRemove);
    if(printable){
      print(toRemove, positions);
    }
    int removed = 1;
    while(removed < N - 1){
      toRemove += rem - 1;//继续数数,减1是因为数据减少了一个
      toRemove %= positions.size();//进行规整
      positions.remove(toRemove);
      removed++;
      if(printable){
        print(toRemove, positions);
      }
    }
    return positions.get(0);
  }



  public static void print(int toRemove, int[] nums) {
    System.out.print((toRemove+1)+" : ");
    for (int i = 0; i < nums.length; i++){
      if(nums[i] == 0)continue;
      System.out.print(nums[i]);
    }
    System.out.println();
  }

  public static void print(int toRemove, List<Integer> list) {
    System.out.print((toRemove+1)+" : ");
    for (int pos : list){
      if(pos == 0)continue;
      System.out.print(pos);
    }
    System.out.println();
  }

11.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: jancyu2008
Posted on: 2008-06-28 00:24

這嗰媞經典啲约瑟夫环笩碼:
public class JosephCircle {    
private class Node {
public Node nextnode;

public boolean flag;
}

private Node[] list;

public JosephCircle() {
list = new Node[30];
int i;
for (i = 0; i < 30; i++) {
Node node = new Node();
node.flag = true;
if (i > 0) {
list[i - 1].nextnode = node;
}
list[i] = node;
}
list[i - 1].nextnode = list[0];
}

public void start() {
Node node = list[29];
for (int i = 0; i < 135; i++) {
if ((i+1) % 8 == 0) {
node.flag = false;
node.nextnode = node.nextnode.nextnode;
} else {
node = node.nextnode;
}
}
}

public void print() {
Node node = list[29];
for (int i = 0; i < 30; i++) {
if (node.flag) {
System.out.print("#");
} else {

System.out.print("@");
}
node = node.nextnode;
}
}

public static void main(String[] args) {
JosephCircle jc = new JosephCircle();
jc.start();
jc.print();
}
}

12.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: 54powerman
Posted on: 2008-07-04 23:45

2、3楼 怎么数的,第二轮1就被t出去了。

13.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: rbible
Posted on: 2008-07-06 16:52

8

循环数数,遇五删数,向下重新再数

14.Re:一个csdn上面的一道小题 [Re: stream] Copy to clipboard
Posted by: danielchirs
Posted on: 2008-08-22 23:21

答案是8


   Powered by Jute Powerful Forum® Version Jute 1.5.6 Ent
Copyright © 2002-2021 Cjsdn Team. All Righits Reserved. 闽ICP备05005120号-1
客服电话 18559299278    客服信箱 714923@qq.com    客服QQ 714923