当前位置: 代码迷 >> C# >> 请问一个使用c#平均分水果的程序或思路
  详细解决方案

请问一个使用c#平均分水果的程序或思路

热度:6   发布时间:2016-05-05 03:52:30.0
请教一个使用c#平均分水果的程序或思路
请教一个使用c#平均分水果的程序或者思路
程序同时满足下面的情景
情景1:
假如有3个人A、B、C, A手里有0个,B手里有0个,C手里有0个水果
假设要分5个水果给这三个人,确保每个人手里的水果尽量平均,最后分完后A手里有2个,B手里有2个,C手里有1个
情景2:
假如有3个人A、B、C, A手里有1个,B手里有3个,C手里有0个水果
假设要分1个水果给这三个人,确保每个人手里的水果尽量平均,最后分完后A手里有1个,B手里有3个,C手里有1个

非常感谢!




------解决思路----------------------
public class Person
        {
            public int AppleNow { get; set; }
            public int AppleAdd { get; set; }
        }

        static void AllotApple(List<Person> persons, int totalApples)
        {
            while (totalApples > 0)
            {
                var person = persons.OrderBy(x => x.AppleNow + x.AppleAdd).First();
                person.AppleAdd++;
                totalApples--;
            }
            persons.ForEach(x =>
            {
                Console.WriteLine(x.AppleAdd);//这就是你要的分配数据
                Console.WriteLine(x.AppleAdd + x.AppleNow);//这是最终分配时每个人的苹果总数量
                Console.WriteLine("##################");
            });
        }

都这样子了还不会改下代码啊……
List<Person> persons = new List<Person>() { new Person() { AppleNow = 1 }, new Person() { AppleNow = 5 }, new Person() };
            AllotApple(persons, 20);
  相关解决方案