C# 关于带完全数的自动填充函数
我必须制作一个模拟歌唱比赛的节目。我几乎完成了这个问题,但我需要一个更现实的函数,它能在一个数组中填充1000到20个人 我试着做一个无限循环,直到成功,但这需要很多时间,所以我删除了它,我试着用0,1001的票数随机填充,直到现在为止,但不幸的是,大多数的票数都是前两个,然后所有的票数都是零C# 关于带完全数的自动填充函数,c#,C#,我必须制作一个模拟歌唱比赛的节目。我几乎完成了这个问题,但我需要一个更现实的函数,它能在一个数组中填充1000到20个人 我试着做一个无限循环,直到成功,但这需要很多时间,所以我删除了它,我试着用0,1001的票数随机填充,直到现在为止,但不幸的是,大多数的票数都是前两个,然后所有的票数都是零 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Thr
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApp0
{
class Program
{
static void Main(string[] args)
{
int[] A = new int[20];
Random rnd = new Random();
int jmo3 = 0;
for (int i = 0; i < A.Length; i++)
{
if (i == A.Length - 1)
A[i] = 1000 - jmo3; // so it can fill the last spot with the rest of the votes
else
{
A[i] = rnd.Next(0, 1001 - jmo3);
jmo3 += A[i];
}
}
}
}
}
我需要的数字是正常的,与第20个家伙一样有机会赢,就像第一个一样:如果你想在一个项目中的所有项目之间获得1000票
还是你的意思完全不同 我不理解一个不以OOP主体开头的C类 如果定义了一个竞赛类来处理投票等所有机制,那么这个问题就更有意义了 有一个名为Votes的数组,其中包含每个参与者的总投票数。要投票给某人,请增加数组中的值。这样做1000次,你就有1000张选票分配给参与者
public class Contest
{
static readonly Random rng = new Random();
public Contest(int n_participants)
{
this.Votes = new int[n_participants];
}
public int Participants => Votes.Length;
public int[] Votes { get; }
public int TotalVotes => Votes.Sum();
public void VoteFor(int index)
{
this.Votes[index] += 1;
}
public void VoteRandom()
{
int index = rng.Next(0, Votes.Length);
VoteFor(index);
}
}
static class Program
{
static void Main(string[] args)
{
// Singing contest with 20 participants.
var singing_contest = new Contest(20);
// Gather 1000 random votes
for (int i = 0; i < 1000; i++)
{
singing_contest.VoteRandom();
}
// Tally up the votes
for (int i = 0; i < singing_contest.Participants; i++)
{
Console.WriteLine($"Participant# : {i+1}, Votes = {singing_contest.Votes[i]}");
}
Console.WriteLine($"Total Votes = {singing_contest.TotalVotes}");
Console.WriteLine("Press [Enter] to exit.");
Console.ReadLine();
}
}
我不确定什么都懂,如果我错了就告诉我。你有20名歌手,所以是整数[20]和1000票?你想让这1000张选票分享给20名歌手吗?5位歌手{19720194199209}的情况?是的,你们节目的目标是什么?谁是赢家?问题是什么?所以,现在选择一个介于0和数组19的最大索引之间的随机数,并递增该数组位置整数。重复1000次。你现在已经随机收集了1000张选票是吗?如果您的意思是直到有人收到1000张选票,请进行选择,直到您刚刚增加的数组位置等于1000。请使用一种填充1000到20人的实际函数澄清您的意思。什么是现实?什么使它变得不现实?你有1000个什么和20个什么?1000到20个人是模棱两可的。
int[] A = new int[20];
Random rnd = new Random();
int index;
// repeat
do
{
// select a random singer (by his index)
index = rnd.Next(0, A.Length);
// add one vote for this singer
A[index]++;
}
// until one singer has 1000 votes
while(A[index] < 1000)
public class Contest
{
static readonly Random rng = new Random();
public Contest(int n_participants)
{
this.Votes = new int[n_participants];
}
public int Participants => Votes.Length;
public int[] Votes { get; }
public int TotalVotes => Votes.Sum();
public void VoteFor(int index)
{
this.Votes[index] += 1;
}
public void VoteRandom()
{
int index = rng.Next(0, Votes.Length);
VoteFor(index);
}
}
static class Program
{
static void Main(string[] args)
{
// Singing contest with 20 participants.
var singing_contest = new Contest(20);
// Gather 1000 random votes
for (int i = 0; i < 1000; i++)
{
singing_contest.VoteRandom();
}
// Tally up the votes
for (int i = 0; i < singing_contest.Participants; i++)
{
Console.WriteLine($"Participant# : {i+1}, Votes = {singing_contest.Votes[i]}");
}
Console.WriteLine($"Total Votes = {singing_contest.TotalVotes}");
Console.WriteLine("Press [Enter] to exit.");
Console.ReadLine();
}
}