帮忙解决一道逻辑题
我在试图找出这个问题背后的逻辑时遇到了很大的困难。我已经开发了其他所有内容,但对于我所坚持的部分,我确实需要一些帮助,任何类型的帮助。
背景故事:
*一群演员围成一圈等待。他们“数 关闭“不同的金额。最后几个试镜 被认为最有机会获得 零件并成为明星。
演员不是有名字,而是有身份 按数字。表中的“试镜顺序”告诉我们, 从左到右阅读,演员的“名字” 将按表演顺序进行试镜。*
示例输出:
等,一直到 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);
}
}
}
}
我所坚持的部分是进行一些数学运算来执行此操作: 替代文本http://content.screencast.com/users/SidSinister/folders/Jing/media/0d178ed4-64bd-468c-acc3-872fa8d8d541/2010-03-17_2035.png
任何人都可以提供一些指导和/或示例代码?
进展一
新代码
public string Order(int inter, int num) { //多变的 字符串结果=“”; int 位置 = 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;
I'm having a great deal of difficulty trying to figure out the logic behind this problem. I have developed everything else, but I really could use some help, any sort of help, on the part I'm stuck on.
Back story:
*A group of actors waits in a circle. They "count
off" by various amounts. The last few to audition
are thought to have the best chance of getting the
parts and becoming stars.
Instead of actors having names, they are identified
by numbers. The "Audition Order" in the table tells,
reading left-to-right, the "names" of the actors who
will be auditioned in the order they will perform.*
Sample output:
etc, all the way up to 10.
What I have so far:
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);
}
}
}
}
The part I'm stuck on is getting some math going that does this:
alt text http://content.screencast.com/users/SidSinister/folders/Jing/media/0d178ed4-64bd-468c-acc3-872fa8d8d541/2010-03-17_2035.png
Can anyone offer some guidance and/or sample code?
PROGRESS ONE
New code
public string Order(int inter, int num)
{
//Variable
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;
Issue: Actors are off by one:
alt text http://content.screencast.com/users/SidSinister/folders/Jing/media/6bb7ab47-9d23-47fb-8691-649127afc47b/2010-03-17_2313.png
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
基本思想是你用公式找到下一个人
,并且每次迭代都会减少一个人。
这是Python 中的。 “count”是 2,因为我们从零开始计数,这使得几乎所有涉及模数的问题都变得更简单。
给予
The basic idea is you find the next person with the formula
And every iteration has one fewer persons in it.
Here it is in python. "count" is 2, because we start counting at zero, which makes almost every problem involving modulo a bit simpler.
giving