问题:
求一个高效算法:
有100名小朋友,
现在有一堆礼物
若是礼物数量小于100,比如为81,则这个81份礼物随机分配给小朋友,但要确保是一个人只能拿到一份礼物; 若是礼物数量大于100,
比如为115,则每个人至少有一份礼物,部分人有两份礼物,但没人拿到两份份以上的礼物;
比如为245,则每个人至少有两份礼物,部分人有三份礼物,但没人拿到三分份以上的礼物; 就是这个思路,这里声明不是大学老师布置的题目啊,是自己工作中一个抽象出来的问题, 希望得到大家的思路,,参与者,高分重谢! 也是个大家交流的机会
求一个高效算法:
有100名小朋友,
现在有一堆礼物
若是礼物数量小于100,比如为81,则这个81份礼物随机分配给小朋友,但要确保是一个人只能拿到一份礼物; 若是礼物数量大于100,
比如为115,则每个人至少有一份礼物,部分人有两份礼物,但没人拿到两份份以上的礼物;
比如为245,则每个人至少有两份礼物,部分人有三份礼物,但没人拿到三分份以上的礼物; 就是这个思路,这里声明不是大学老师布置的题目啊,是自己工作中一个抽象出来的问题, 希望得到大家的思路,,参与者,高分重谢! 也是个大家交流的机会
首先计算没人至少几分,最多几份!
可以声请string[a] arr保存礼物,a为礼物数量,a/100为至少份数设为min,a%100!=0 最多max=++min;否则max=min;
然后取随机数,第一次取0-(a-1)之间的一个随机数,假设b,则将arr[b]与arr[a-1]位置互换!(意识是arr[b]这个礼物已经被分配,下此取随机数是从0-(a-1)之间取,然后与arr[a-2]互换),以此类推,取到min个分配给一个人,当所有人都分配完后,剩下的,人手一份,随机取人
j = 礼物数 / 100
随即选i个人,这i个人是j+1个
没选到的人是j个
static void Main(string[] args)
{
Gift(100, 245);
} static void Gift(int people, int gift)
{
if (people > gift)
{
bool[] temp = new bool[people];
Random r = new Random(Environment.TickCount);
while (gift > 0)
{
int i = r.Next(0, people);
if (!temp[i])
{
temp[i] = true;
gift--;
}
}
for (int i = 0; i < people; i++)
{
if (temp[i])
Console.WriteLine("第{0}位得到了1份礼物", i + 1);
}
}
else if (people == gift)
{
Console.WriteLine("每人都拿到一份礼物");
}
else
{
int each = gift / people;
int left = gift % people;
bool[] temp = new bool[people];
Random r = new Random(Environment.TickCount);
while (left > 0)
{
int i = r.Next(0, people);
if (!temp[i])
{
temp[i] = true;
left--;
}
}
for (int i = 0; i < people; i++)
{
Console.WriteLine("第{0}位得到了{1}份礼物", i + 1, each + (temp[i] ? 1 : 0));
}
}
}
int sum = 234;
int aa = 100;
string randomCode = "";
ArrayList li = new ArrayList();
for (int i = 1; i <= aa; i++)
{
li.Add(i);
}
int num = sum / aa;
int ys = sum % aa;
Random rand = new Random();
for (int j = 1; j <= ys; j++)
{
rand = new Random(j * ((int)DateTime.Now.Ticks));
int t = rand.Next(aa);
randomCode += li[t]+",";
li.RemoveAt(t);
aa--;
}
Response.Write(randomCode);写出来种,各位大哥鉴定鉴定
int sum = 234;
int aa = 100;
string randomCode = "";
ArrayList li = new ArrayList();
for (int i = 1; i <= aa; i++)
{
li.Add(i);
}
int num = sum / aa;
int ys = sum % aa;
Random rand = new Random();
for (int j = 1; j <= ys; j++)
{
rand = new Random(j * ((int)DateTime.Now.Ticks));
int t = rand.Next(aa);
randomCode += li[t]+",";
li.RemoveAt(t);
aa--;
}
Response.Write(randomCode);
//每个人至少有num 个,其中在randomCode中的人有num+1个
using System.Collections.Generic;
using System.Text;namespace gift
{
class Program
{
static void Main(string[] args)
{
int peopleNum = 0;
int giftNum = 0;
Console.WriteLine("请输入要礼物的人数");
string s = Console.ReadLine();
peopleNum = Convert.ToInt32(s);
Console.WriteLine("请输入要发的礼物数");
string g = Console.ReadLine();
giftNum = Convert.ToInt32(g);
dogigt(peopleNum, giftNum); }
public static void dogigt(int peopleNum, int giftNum)
{
int mincount = giftNum / peopleNum;
if (mincount == 0)
{
mincount = 1;
}
int maxcount = 0;
int temp = 0;
int[] people = new int[peopleNum]; List<int> list = new List<int>(); Random ran = new Random();
if (giftNum > peopleNum)
{
maxcount = mincount + 1;
}
else
{
maxcount = mincount;
}
int num = giftNum;
while (num > 0)
{
temp = ran.Next(0, peopleNum);
if (list.Contains(temp))
{
continue;
}
list.Add(temp);
if (list.Count % peopleNum == 0)
{
list.Clear();
}
if (people[temp] < maxcount)
{
people[temp] += 1;
num--;
}
} for (int i = 0; i < people.Length; i++)
{
Console.WriteLine(people[i].ToString());
}
Console.ReadLine();
}
}
}
坚决不用集合,用数组
坚决不用for,用while
坚决不用/,用>>