声振论坛

 找回密码
 我要加入

QQ登录

只需一步,快速开始

查看: 1669|回复: 8

[综合讨论] 出个题大家来算一下

[复制链接]
发表于 2007-7-23 23:11 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?我要加入

x
大家有兴趣可以算一下
其实可以编个小程序 语言不限 就当个小练习吧
有1500个人,围成一圈,从第1个人开始报数,1 2 3 报到3的退出圈子,后面的人又从1 开始
请问当最后圈子里留下一个人的时候,他在原来1500人中的序号是多少

评分

1

查看全部评分

回复
分享到:

使用道具 举报

发表于 2007-7-24 02:44 | 显示全部楼层
905?

[ 本帖最后由 bainhome 于 2007-7-24 03:06 编辑 ]
发表于 2007-7-24 10:11 | 显示全部楼层
clear all; m=zeros(1500); i=1; k=1;
while i<=1500, m(i,1)=i; i=i+1; end
while k<1500, k=k+1;
      if m(3,k-1)==0, m(1,k)=m(2,k-1);
      else
           m(1:1497,k)=m(4:1500,(k-1));
           m((1500-k):(1501-k),k)=m(1:2,k-1);
      end
end
m(1,1500)

[ 本帖最后由 ChaChing 于 2009-12-5 12:31 编辑 ]

评分

1

查看全部评分

发表于 2007-7-24 10:32 | 显示全部楼层

回复 #1 jimin 的帖子

clear all; clc
x= 1:1500; t = mod(length(x),3);
x(3:3:end) = [];
x = [x(end-t+1:end),x(1:end-t)];
while length(x)>3
    t = mod(length(x),3);
    x(3:3:end) = [];
    x = [x(end-t+1:end),x(1:end-t)];
end
x(2)

评分

1

查看全部评分

 楼主| 发表于 2007-7-24 10:46 | 显示全部楼层
答案我算出来是905
发表于 2007-7-24 11:05 | 显示全部楼层
我的方法:
k=1:1500;
while length(k)>2
n=length(k);
k(3:3:end)=[];
k=circshift(k,[0,mod(n,3)]);
end
k(1)=[]

评分

1

查看全部评分

 楼主| 发表于 2007-7-24 18:56 | 显示全部楼层
8错8错
几位的程度都短小精悍
机子上没装上matlab 没去验证了
另贴两个 c# 编的解决次问题的小程序
答案应在结果的基础上加1 程序里是从0开始编号的
using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
    class Program
    {
        
        static void Main(string[] args)
        {
            bool[] arr = new bool[1500];
            for (int i = 0; i < arr.Length ; i++)
            {
                arr[ i ] = true;

            }
            int leftNum = arr.Length;
            int countNum = 0;
            int index = 0;
            while (leftNum > 1)
            {
                if (arr[index] == true)
                {
                    countNum++;
                    if (countNum == 3)
                    {
                        countNum = 0;
                        arr[index] = false;
                        leftNum--;
                    }

                }
                    
                    index++;
                    if (index == arr.Length)
                    {
                        index = 0;
                    }
               
               

            }
            for (int i = 0; i < arr.Length; i++)
            {
                if (arr[ i ] == true)
                    Console.WriteLine(i);
            }
            Console .ReadLine ();
        }
    }
}

[ 本帖最后由 jimin 于 2007-7-24 19:03 编辑 ]
 楼主| 发表于 2007-7-24 18:58 | 显示全部楼层
另可以考虑一下,把每次出圈子的人的按顺序打印出来
这个主要原理是数据结构的双向链表
using System;
using System.Collections.Generic;
using System.Text;

namespace Count3Quit
{
    class Program
    {
        static void Main(string[] args)
        {
            KidCircle kc = new KidCircle(1500);
                    int countNum = 0;
                    Kid k = kc.first;
                    while(kc.count > 1) {
                            countNum ++;
                            if(countNum == 3) {
                                    countNum = 0;
                    Console.Write(k.id + "  ");
                                    kc.delete(k);
                            }
                            k = k.right;
                    }
               
               
            Console.WriteLine(kc.first.id);
            Console.ReadLine();

            }
    }

   

    public class KidCircle
    {
        public int count = 0;
        public Kid first, last;

       public  KidCircle(int n)
        {
            for (int i = 0; i < n; i++)
            {
                add();
            }
        }

        public 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++;
        }

        public 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--;
        }
        
    }
    public class Kid
    {
        public int id;
        public Kid left;
        public Kid right;
    }
}

[ 本帖最后由 jimin 于 2007-7-24 18:59 编辑 ]

评分

1

查看全部评分

发表于 2007-9-2 21:43 | 显示全部楼层
典型的JOSEPH问题!
您需要登录后才可以回帖 登录 | 我要加入

本版积分规则

QQ|小黑屋|Archiver|手机版|联系我们|声振论坛

GMT+8, 2024-5-13 03:03 , Processed in 0.070571 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表