在奴隶社会奴隶主选怎么聪明奴隶的问题:
让所有奴隶站成以圈有m个奴隶
奴隶主随机出个数。n
奴隶自己报数。如果自己报的数为n怎被杀掉,其后一位奴隶从第一个数开始报,直到第二个被杀掉最后剩下一为奴隶
让所有奴隶站成以圈有m个奴隶
奴隶主随机出个数。n
奴隶自己报数。如果自己报的数为n怎被杀掉,其后一位奴隶从第一个数开始报,直到第二个被杀掉最后剩下一为奴隶
解决方案 »
- 关于C#类(.cs)的问题
- 对于WinFrom窗体的IsMdiContainer属性的一个疑问
- .net文件上传
- 关于服务器控件的若干疑问
- 如何把生成的html页面转换为pdf文件格式并保存到本地机子上
- winform picturebox显示gif 怎么能不循环播放?在IE中查看GIF只播放一次。急!
- 如何在WinForm的不同窗体间传输数据啊?
- DataView dv = new DataView(ds1.Tables["table1"]) 不断的这样创建 对性能有没有影响?
- 急,急,急!如何弹出话框???
- vscode
- DataGridView的数据源问题
- C#中怎么连接sybase iq数据库做操作?
list<string> 奴隶们 = new list<string>();
int pos = 0;public void kill奴隶(int n)
{
for (int i = 0; i < n; i++)
{
Pos自增函数();
}
奴隶们.RemoveAt(pos);//杀死奴隶
}public void Pos自增函数()
{
if (pos == 奴隶们.count - 1)
{
pos = 0;
}
else
{
pos = pos + 1;
}
}
{
while (奴隶们.Count > 1)
{
kill奴隶(n);
}
}
class Test
{
public List<string> nulimen = new List<string>();
int pos = 0; public Test ()
{
for (int i = 0; i < 100; i++)
{
nulimen.Add(i.ToString());
}
} public void 运行 (int n)
{
while (nulimen.Count > 1)
{
kill奴隶(n);
}
} public void kill奴隶 (int n)
{
for (int i = 0; i < n; i++)
{
Pos自增函数();
}
nulimen.RemoveAt(pos);//杀死奴隶
Console.WriteLine("杀死奴隶" + pos.ToString());
} public void Pos自增函数 ()
{
if (pos >= nulimen.Count - 1)
{
pos = 0;
}
else
{
pos = pos + 1;
}
}
}class Program
{
static unsafe void Main (string[] args)
{ Test tt = new Test();
tt.运行(10);
Console.WriteLine("存活奴隶" + tt.nulimen[0]);
}
}
namespace WindowsApplication26
{
public partial class Form1 : Form
{
int CurrentIndex = 0,Count=1;
System.Collections.ArrayList Total = new System.Collections.ArrayList();
int Kill = 6; public Form1()
{
InitializeComponent(); for (int i=1;i<=100;i++)
Total.Add(i); StartKill(ref CurrentIndex, ref Count);
MessageBox.Show(Total[0].ToString()+"活着"); // 69
} void StartKill(ref int CurrentIndex, ref int Count)
{
if (Total.Count == 1)
return;
if (++CurrentIndex == Kill)
{
Total.RemoveAt(Count);
CurrentIndex = 0;
}
else
Count = ++Count % Total.Count ;
StartKill(ref CurrentIndex, ref Count);
}
}
}
{
int r=0;//最后剩下的那个人
for (i=2;i<=m;i++)
r=(r+n)%i;
return r+1;
}
终止条件是M=1有点意思
static void Main(string[] args)
{
try
{
Console.Write("请输入奴隶个数: ");
int i = Convert.ToInt32(Console.ReadLine());
Console.Write("处决几号奴隶: ");
int j = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入间隔: ");
int k = Convert.ToInt32(Console.ReadLine());
List<int> lst1 = new List<int>();
for (int x = 1; x < i + 1; x++)
{
lst1.Add(x);
}
Run:
for (int y = j; lst1.Count!=1; y = y + k + 1)
{
int z = lst1.Count;
if (y<=lst1.Count || lst1.Count != 1)
{
lst1.Remove(y);
if(z==lst1.Count)
{
j = lst1[0];
goto Run;
}
}
else
{
j = lst1[0];
goto Run;
}
}
foreach (int p in lst1)
{
Console.WriteLine("最后幸存的是{0}号奴隶。", p);
}
}
catch
{
Console.WriteLine("输入错误,只能输入数字。");
}
}
using System.Text; namespace CshapeJosephus
{
public class Program
{
static void Main(string[] args)
{
Console.Write("请输入士兵个数: ");
int n = Convert.ToInt32(Console.ReadLine());
Console.Write("从几号士兵开始: ");
int k = Convert.ToInt32(Console.ReadLine());
Console.Write("请输入间隔: ");
int m = Convert.ToInt32(Console.ReadLine());
Program p = new Program();
p.Jose(n, k, m);
}
public void Jose(int total,int start,int alter) //total为总人数,start为第一个开始数数的人,数到alter就出局
{
int j,k=0;
int y; //s数组存储初始数据
int[] s = new int[total+1];
s[0] = total;
//count数组存储按出列顺序的数据,以当结果返回
int[] counter = new int[total+2];
//对数组s赋初值,第一个人序号为1,第二人为2,依此下去
for (int i = 1; i <=total; i++)
{
s[i] = i;
} //按出列次序依次存于数组count中
for (int i = total; i >= 1; i--)
{
start = (start + alter - 1) % i;
counter[k] = s[start];
k++;
for (j = start + 1; j <= i; j++)
{
s[j - 1] = s[j];
}
} //依次输出淘汰的人
Console.Write("依次被杀的士兵是:");
for (int i = 0; i < k-1; i++)
{
Console.Write("{0} ", counter[i]);
}
Console.WriteLine();
Console.WriteLine("最后留下的士兵是第{0}个",counter[k-1]);
}
}
}