出个题大家来算一下
大家有兴趣可以算一下其实可以编个小程序 语言不限 就当个小练习吧
有1500个人,围成一圈,从第1个人开始报数,1 2 3 报到3的退出圈子,后面的人又从1 开始
请问当最后圈子里留下一个人的时候,他在原来1500人中的序号是多少 905?
[ 本帖最后由 bainhome 于 2007-7-24 03:06 编辑 ] 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 jimin 的帖子
clear all; clcx= 1:1500; t = mod(length(x),3);
x(3:3:end) = [];
x = ;
while length(x)>3
t = mod(length(x),3);
x(3:3:end) = [];
x = ;
end
x(2) 答案我算出来是905 我的方法:
k=1:1500;
while length(k)>2
n=length(k);
k(3:3:end)=[];
k=circshift(k,);
end
k(1)=[] 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;
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 == true)
{
countNum++;
if (countNum == 3)
{
countNum = 0;
arr = 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 编辑 ] 另可以考虑一下,把每次出圈子的人的按顺序打印出来
这个主要原理是数据结构的双向链表
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;
publicKidCircle(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 编辑 ] 典型的JOSEPH问题!
页:
[1]