有一千个人,开始报数,按1,2,3 报数,报1的人被舍弃,问最后留下的是原来一千人中的哪一位(一千个人是首位相接围成一个圈,每一轮都不是从头开始的)大家说说思路了,要个比较简单的方法

解决方案 »

  1.   

        public struct person
        {
            public int realindex;
            public int currentindex;
        }private void getperson()
            {
                int TotalPerosons = 1000;
                ArrayList persons = new ArrayList();
                for (int i = 0; i < TotalPerosons; i++)
                {
                    person oneperson = new person();
                    oneperson.realindex = i;
                    oneperson.currentindex = i;
                    persons.Add(oneperson);
                }            while (persons.Count > 1)
                {
                    for (int i = 0; i < persons.Count; i++)
                    {
                        person cuperson = (person)persons[i];
                        if (cuperson.currentindex % 3 == 0)
                        {
                            persons.Remove(cuperson);
                        }
                    }                for (int j = 0; j < persons.Count; j++)
                    {
                        person cuperson = (person)persons[j];
                        cuperson.currentindex = j;
                        persons[j] = cuperson;
                    }
                }
                person Lastperson = (person)persons[0];
                MessageBox.Show(Lastperson.realindex.ToString());
                
            }