# 猴子选大王的问题

csszhz 2009-04-18 11:13:57

...全文
472 点赞 收藏 19

19 条回复

qingronglu 2010-12-21
#include <stdio.h>
#include <malloc.h>

struct monkey{
int num;
struct monkey* next;
};
int KingOfMonkeys(struct monkey* head, int m);

{
int i=0;
struct monkey *temp=NULL;
struct monkey *newNode=NULL;
for(i=0; i<n; i++)
{
{
}
else
{
newNode=(struct monkey*)malloc(sizeof(struct monkey));
newNode->num=i+1;
temp->next=newNode;
temp=newNode;
}
}
}

int KingOfMonkeys(struct monkey* head, int m)
{
int i=0;
for(i=1;temp->next!=temp;i++)
{
if(i%m==0)
{
{
}
free(temp);
}
else
{
temp=temp->next;
}
}
return temp->num;
}

void main()
{
int m, n, king;//定义m,n还有最后的大王
printf("Please enter the number of the monkeys(m):\n");
scanf("%d",&m);//输入猴子数
printf("Please enter the value of n:\n");
scanf("%d",&n);//输入N
/*确保输入的m大于n的值且符合题意*/
if(m<n||m==n||m==0||n==0||m<0||n<0)
printf("Wrong enter!\n");
else
{
printf("The king of the monkeys is number: %d\n", king);
}
}

SimonYeung 2009-04-20

arlande 2009-04-19
import java.util.*;
class Monkey{
public static void main(String[] args){

ArrayList <Integer> arr = new ArrayList <Integer>(1000);

for (int i = 0; i < 1000; i++) {
}

while (arr.size() > 1) {

int count = 0;
for (int i = 0; i < arr.size(); i++) {
if (++count % 2 == 1) {
arr.set(i, -1);
}
}

for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == -1){
arr.remove(i);
}
}

}

System.out.println("最后剩下的猴子的编号为:" + arr.get(0));

}
}
count 应该放在while 循环内部。如上编程，最后为正解511

Steve 2009-04-19

``````public class MonkeyKing
{
private static final int N = 1000;
public static void main(String[] args) {
int a [] = new int[N+1]; // a[0]表示存活的第一只猴子的位置,位置从1-1000, 输出时-1对应0-999
for (int i=0; i<=N; i++)
a[i] = i+1; //a[i]表示编号为i-1的猴子紧邻的后一个猴子位置

int num = N;
while (num>1) //num表示存活猴子数
{
int startPos = 0;

while (a[startPos]<N)
{
a[startPos] = a[a[startPos]]; //跳过一只猴子
startPos = a[startPos]; //向下移动两个位置
}

num = num / 2; //猴子数减半
}
System.out.println(a[0]-1); // 输出结果
}
}
``````

Steve 2009-04-19
[Quote=引用 14 楼 csszhz 的回复:]

[/Quote]

csszhz 2009-04-19
[Quote=引用 10 楼 runffer_yang 的回复:]

Java code
System.out.println(511);
[/Quote]

Steve 2009-04-19
[Quote=引用 11 楼 jinxfei 的回复:]

[/Quote]

csszhz 2009-04-19
6楼的代码不能直接用，照下面倒是编译通过了。
``````package hzxdw;
import java.util.ArrayList;
public class chooseKing {

public void run() {
ArrayList <Integer> arr = new ArrayList <Integer>(1000);

for (int i = 0; i < 1000; i++) {
}
int count = -1;
while (arr.size() > 1) {

for (int i = 0; i < arr.size(); i++) {
if (++count % 2 == 1) {
arr.set(i, -1);
}
}

for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == -1){
arr.remove(i);
}
}
}

System.out.println("最后剩下的猴子的编号为:" + arr.get(0));
}
}``````

``````package hzxdw;

public class Test {
public static void main(String[] args) {
chooseKing k=new chooseKing();
k.run();
}
}``````

jinxfei 2009-04-19

Steve 2009-04-19

``````
System.out.println(511);``````

csszhz 2009-04-19

ny_shaoer 2009-04-19

``System.out.println( Arrays.toString( chooseKing( a ) )  );``

ny_shaoer 2009-04-19
``````
public static int[] chooseKing( int a[] )
{
if ( a.length == 1 )
return a;

int[] b = new int[ a.length / 2 ];
int count = 0;

for ( int i = 0; i < a.length; i++ )
{
if ( i % 2 != 0 )
{
b[count] = a[i];
count++;
}
}
return chooseKing( b );
}

``````

zhongyucai 2009-04-19
public class Monkey{
public static void main(String[] args){
public void run() {
ArrayList <Integer> arr = new ArrayList <Integer>(1000);

for (int i = 0; i < 1000; i++) {
}
int count = -1;
while (arr.size() > 1) {

for (int i = 0; i < arr.size(); i++) {
if (++count % 2 == 1) {
arr.set(i, -1);
}
}

for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == -1){
arr.remove(i);
}
}
}

System.out.println("最后剩下的猴子的编号为:" + arr.get(0));
}

itliyi 2009-04-19

jinxfei 2009-04-19
[Quote=引用 1 楼 jinxfei 的回复:]

Java code
Vector v=new Vector();
for(int i=0;i<1000;i++){
}

while(v.size>1){
int index=0;
for(Iterator iter=v.iterator;iter.hasNext();){
if ((index % 2)==1){
iter.remove();
}
index++;
}
}

System.out.println(v.get(0));
[/Quote]

yjfeng1986 2009-04-19
``````
public void run() {
ArrayList <Integer> arr = new ArrayList <Integer>(1000);
// 初始编号1至1000
for (int i = 0; i < 1000; i++) {
}
int count = -1;// 计数值
while (arr.size() > 1) {//最后只剩下1只猴子
//编号为奇数时赋值为-1（做记号）
for (int i = 0; i < arr.size(); i++) {
if (++count % 2 == 1) {
arr.set(i, -1);
}
}
//将记号为-1的值从列表中删除
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i) == -1){
arr.remove(i);
}
}
}
// 输出剩下结果
System.out.println("最后剩下的猴子的编号为:" + arr.get(0));
}

``````

jinxfei 2009-04-19

``````
Vector v=new Vector();
for(int i=0;i<1000;i++){
}

while(v.size>1){
int index=0;
for(Iterator iter=v.iterator;iter.hasNext();){
if ((index % 2)==1){
iter.remove();
}
}
}

System.out.println(v.get(0));
``````

Eclipse

5.8w+

Java Eclipse

2009-04-18 11:13