【发布时间】:2022-06-17 22:46:19
【问题描述】:
我创建了一个程序,它显示 1000 (1k) 个随机整数,其 rng 范围是 1 - 1000,我想知道以最高和最低频率生成特定数字的次数和显示它。例:51是已经生成50次的数字,是最高的 (注意:我不能使用任何内置函数来构建逻辑)
{
List<int> numPool = new List<int>();
Random rnd = new Random();
string uinput = "";
int storage = 0;
while (true)
{
// generating number pool
for (int i = 0; i < 1000; i++)
numPool.Add(i + 1);
// generating 100 numbers
Console.WriteLine("Generating 100 numbers (1-1001) with duplicates...");
int d = 1;
while (numPool.Count > 0)
{
int temp = 0;
temp = rnd.Next(0, numPool.Count); // this generates a random index between 0 and however long the list is
Console.Write("{0}\t", temp);
numPool.RemoveAt(temp); // removes the index
if (d % 10 == 0)
Console.WriteLine();
d++;
}
Console.WriteLine("Highest amount of duplicate numbers: "); // this part should show the number with the highest amount of duplicates
Console.WriteLine("Number of times it was duplicated: "); // this part will show how many times it was duplicated
Console.WriteLine("\nLeast amount of Duplicate numbers: "); // this part should show the number with the least amount of duplicates
Console.WriteLine("Number of times it was duplicated: "); // this part will show how many times it was duplicated
Console.Write("\nWhat number would you like to show the frequency?: ");
uinput = Console.ReadLine();
storage = int.Parse(uinput);
// from this part it should show the number of times the duplicate number has appeared according to the user input
//Console.WriteLine("The number " + storage + " has appeared " + insert the amount of times it has appeared + " times.");
Console.ReadKey();
Console.Clear();
}
}
【问题讨论】:
-
它并没有真正生成数字 1000,因为它必须是 1001。但如果它不生成它完全没问题。重要的是在不使用“ANY”内置函数的情况下显示重复次数最多和最少的数字。
-
创建一个长度为1000的数组,用零初始化。然后对于随机数 n 递增数组字段 a[n]。最后,数组存储了哪个数字被随机化的频率。遍历数组并寻找最大的条目。
-
我只能为这个使用一个列表,这是我自己的挑战。如果不可能,那么我将使用数组@FlorianMetzger-Noel