把犯人围城一圈,每次杀掉第七个,又从第八个开始杀掉第七个,直到剩下最后一个
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//假设有10个人
List<int> list = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
//用于记录谁是第几个
int index = 1;
//开始循环的杀人,数一次index+1
for (int i = 0; i < list.Count; i++) {
//如果index=7那么对应的这个人将会被杀死
if (index == 7){
Console.WriteLine("杀死:"+list[i]);
//此处代码是将数到的第七个杀死之后,那么重新排序数组
//然后index的计数重新开始计算,i=-1,也开始重新循环
//无法用程序把这10人围城一个圈,那么就把这10人在圈的位置,重新组合
//比如,第一次游戏开始,10人的第一次位置是1,2,3,4,5,6,7,8,9,10
//杀死位于第七个的人7之后,下一个是从8开始,这次的位置就是8,9,10,1,2,3,4,5,6
//这样依次类推,杀第七个,下面的代码就是这样去重新组合新数组
List<int> temp = new List<int>();
for (int j = i+1; j < list.Count; j++){
temp.Add(list[j]);
}
foreach (int g in temp){
list.Remove(g);
}
//移除掉第七个
list.Remove(list[i]);
temp.AddRange(list);
list.Clear();
//新数组
list = temp;
//重新循环计算
index = 0;
i = -1;
}
//如果只剩下最后一个则跳出循环
if (list.Count == 1)
{
Console.WriteLine("最后活下来的人是:" + list[0]);
break;
}
//假如人还没死到最后一个则继续杀
if (i == list.Count - 1){
i = -1;
}
index++;
}
}
}
}