我在试图弄清楚这个问题背后的逻辑时遇到了很大的困难。我已经开发了其他所有东西,但我真的可以在我坚持的部分使用一些帮助,任何形式的帮助。
背景故事:
*一群演员在一个圆圈中等待。他们“算了 “按各种金额计算。最后几个试镜 被认为有最好的机会获得 零件和成为明星。
而不是演员有名字,他们被识别 按数字。表中的“Audition Order”告诉我们, 从左到右阅读,演员们的“名字” 将按照他们将要执行的顺序进行试镜。*
示例输出:
等,一直到10。
到目前为止我所拥有的:
using System;
using System.Collections;
using System.Text;
namespace The_Last_Survivor
{
class Program
{
static void Main(string[] args)
{
//Declare Variables
int NumOfActors = 0;
System.DateTime dt = System.DateTime.Now;
int interval = 3;
ArrayList Ring = new ArrayList(10);
//Header
Console.Out.WriteLine("Actors\tNumber\tOrder");
//Add Actors
for (int x = 1; x < 11; x++)
{
NumOfActors++;
Ring.Insert((x - 1), new Actor(x));
foreach (Actor i in Ring)
{
Console.Out.WriteLine("{0}\t{1}\t{2}", NumOfActors, i, i.Order(interval, x));
}
Console.Out.WriteLine("\n");
}
Console.In.Read();
}
public class Actor
{
//Variables
protected int Number;
//Constructor
public Actor(int num)
{
Number = num;
}
//Order in circle
public string Order(int inter, int num)
{
//Variable
string result = "";
ArrayList myArray = new ArrayList(num);
//Filling Array
for (int i = 0; i < num; i++)
myArray.Add(i + 1);
//Formula
foreach (int element in myArray)
{
if (element == inter)
{
result += String.Format(" {0}", element);
myArray.RemoveAt(element);
}
}
return result;
}
//String override
public override string ToString()
{
return String.Format("{0}", Number);
}
}
}
}
我坚持的部分是得到一些数学运算,这样做: alt text http://content.screencast.com/users/SidSinister/folders/Jing/media/0d178ed4-64bd-468c-acc3-872fa8d8d541/2010-03-17_2035.png
有人可以提供一些指导和/或示例代码吗?
PROGRESS ONE
新代码
public string Order(int inter,int num) { //变量 string result =“”; int pos = 0; ArrayList myArray = new ArrayList();
//Filling Array
for (int i = 0; i < num + 1; i++)
myArray.Add(i+1);
while (myArray.Count > 1)
{
pos = (pos + inter) % myArray.Count;
result += (myArray[pos] + " ");
myArray.RemoveAt(pos);
}
result += (myArray[0]);
myArray.Clear();
return result;
答案 0 :(得分:2)
基本的想法是找到具有公式的下一个人
next position = (current position + count) modulo number of people
每次迭代都会少一些人。
这是在python中。 “count”是2,因为我们从0开始计数,这使得几乎所有涉及模数的问题都变得更简单。
people=[1,2,3,4,5]
people=['a','b','c','d','e']
count=2 # base 0 counting
pos=0
while len(people) > 1:
pos = (pos + count) % len(people)
print "at pos",pos,"eliminating person",people[pos],'from',people,
del people[pos]
print 'leaving',people
print 'winner is',people[0]
给
at pos 2 eliminating person c from ['a','b','c','d','e'] leaving ['a','b','d','e']
at pos 0 eliminating person a from ['a','b','d','e'] leaving ['b','d','e']
at pos 2 eliminating person e from ['b','d','e'] leaving ['b','d']
at pos 0 eliminating person b from ['b','d'] leaving ['d']
winner is d