首页 新闻 赞助 找找看

使用循环解决问题

0
[已解决问题] 解决于 2013-04-30 15:45

19个人围圈报数,遇3退出,最后剩下哪个人?用面向对象的方式做。。

刺刀A的主页 刺刀A | 菜鸟二级 | 园豆:204
提问于:2013-04-15 18:31
< >
分享
最佳答案
0

写得有点乱 你看看是不是你要的:

   protected void Page_Load(object sender, EventArgs e)         {             //初始化19个要报数的人             List<ActionUser> userList = new List<ActionUser>();             for (int i = 1; i <= 19; i++)             {                 userList.Add(new ActionUser { No = i, State = 0 });             }             var activeUser = SoundOff(userList);             var a = 0;         }

        /// <summary>         /// 报数         /// </summary>         private static ActionUser SoundOff(List<ActionUser> userList)         {             int startNo = 1;//开始报数的起始数,第一次为1             while (userList.Where(o => o.State == 0).Count() > 1)             {//每次开始循环报数的时候,检查是不是只有一个人了                 int activeNo = userList.Where(o => o.State == 0).Count();//设定每次循环报数的参与人数                 int tempSoundOffNum = startNo + activeNo;//设定每次循环报数的截止报数                 int removeCounts = 0;//定义每次循环退出了的人的数量,补正在本轮循环时,该退出的人下标索引                 for (int i = startNo; i < tempSoundOffNum; i++)                 {                     if (i.ToString().Contains("3"))//遇到报数中包含3的人则退出                     {                         ChangeUserState(userList, i - startNo - removeCounts);                         removeCounts++;                     }                 }                 startNo = startNo + activeNo;//设置下一次报数的开始数值             }             return userList.SingleOrDefault(o => o.State == 0);         }

        /// <summary>         /// 改变用户的状态,设置为已退出         /// </summary>         /// <param name="user"></param>         private static void ChangeUserState(List<ActionUser> userList, int userNo)         {             userList.RemoveAt(userNo);         }     }

    public class ActionUser     {         //编号         public int No { get; set; }

        //状态         public int State { get; set; }     }

奖励园豆:5
sharplizhi | 菜鸟二级 |园豆:366 | 2013-04-18 12:21
class Program
    {
        static void Main(string[] args)
        {
            int count;
            do
            {
                Console.WriteLine("请输入正整数");
            } while (!int.TryParse(Console.ReadLine(), out count) || count < 1);
            //产生指定个数的人
            Person[] persons=new Person[count];
            for (int i = 0 ; i < count ; i++)
            {
                persons[i] = new Person
                {
                    SerialNo = i + 1,
                    InQueue = true
                };
            }
 
            Person.Number = 1;
            Person.Remain = count;
            int j = 0;
            while (Person.Remain > 1)//当剩余人数超过一个时
            {
                if (persons[j].InQueue)//判断当前人在不在队列中
                {
                    if (Person.Number % 3 == 0)//如果当前数到3的倍数
                    {
                        persons[j].InQueue = false;
                        Person.Remain--;
                    }
                    Person.Number++;//数下一个数
                }
                j = ++j % count;//考虑到索引增涨到10,需要回到0
            }
            //当退出上面的循环后,意味着只剩最后一个人
            //那么继续循环,直到找到最后那个在队列中的人
            while (!persons[j].InQueue)
                j = ++j % count;
            Console.WriteLine("最后一个人是:{0}号,他报的数是:{1}", persons[j].SerialNo, Person.Number);
        }
    }
 
    class Person
    {
        //当前人的序号
        public int SerialNo { getset; }
        //当前人是否在队列中,true:在,false:不在
        public bool InQueue { getset; }
        //对所有人而言,当前报到第几个数了
        public static int Number { getset; }
        //所有人中,还剩下几个人
        public static int Remain { getset; }
    }
刺刀A | 园豆:204 (菜鸟二级) | 2013-04-18 15:48
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册