62,046
社区成员
发帖
与我相关
我的任务
分享
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个
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);
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));
}
}
}