假设18个位置(_locId):依次检查位置上的人(函数Check(_locid),返回值的格式是“身份|年龄”)身份:儿童,青年,中年,老年。如果位置上(_locid)是儿童,选取其中年龄最小的儿童送礼物(函数Gift(_locid),参数是位置),如有年龄相同,且都最小,随机选取一个送礼物。

解决方案 »

  1.   

       public class People
        {
            private int age;        public int Age
            {
                get { return age; }
                set { age = value; }
            }        private string status;        public string Status
            {
                get { return status; }
                set { status = value; }
            }        private int no;        public int No
            {
                get { return no; }
                set { no = value; }
            }
            public People(int nowNo, string info)
            {
                no = nowNo;
                string[] s = info.Split('|');
                status = s[0];
                age = int.Parse(s[1]);
            }        public void FindChildren()
            {
                int minAge = 100;
                List<People> l = new List<People>();
                for (int i = 0; i < 18; i++)
                {
                    People p = new People(Check(i));
                    if (p.Status.Equals("儿童"))
                    {
                        if (p.Age < minAge)
                        {
                            l.Clear();
                            l.Add(p);
                        }
                        else if (p.Age = minAge)
                        {
                            l.Add(p);
                        }
                    }
                }            if (l.Count == 0)
                    return;
                if (l.Count == 1)
                    Gift( l[0].No);
                if (l.Count > 1)
                {
                    Random r = new Random();
                    Gift( r.Next(0, l.Count));
                }
            }
        }
      

  2.   

    最后那个 if (l.Count > 1)
                {
                    Random r = new Random();
                    Gift( l[r.Next(0, l.Count)].No);
                } 
      

  3.   

    using System;
    using System.Collections.Generic;
    using System.Text;namespace ConsoleApplication21
    {
        class Program
        {
            class Man
            {
                public int _Locid;
                public String Style;
                public int Age;            public Man(int _Locid, String Style, int Age)
                {
                    this._Locid = _Locid;
                    this.Style = Style;
                    this.Age = Age;
                }
            }        static List<Man> Men = new List<Man>();
            static Man Result = null;        static void Main(string[] args)
            {
                Men.Add(new Man(1, "老人", 80));
                Men.Add(new Man(2, "小孩", 5));
                Men.Add(new Man(3, "小孩", 4));
                Men.Add(new Man(4, "小孩", 7));
                Men.Add(new Man(5, "青年", 23));
                Men.Add(new Man(6, "小孩", 4));            Men.ForEach(delegate(Man M)
                {
                    if (Result == null || M.Age < Result.Age || M.Age == Result.Age && new Random().Next(2) == 1)
                        Result = M;
                });
                Gift(Result);
                Console.Read();
            }        static void Gift(Man M)
            {
                Console.WriteLine(M._Locid + " " + M.Style + " " + M.Age + " 获得礼物");
            }
        }
    }