【问题标题】:How do you display the number that has the lowest and highest amount of duplicates in a list w/o using built in functions?如何使用内置函数显示列表中重复次数最少和最多的数字?
【发布时间】: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

标签: c# list random


【解决方案1】:

您可以遍历列表并逐个选择每个数字,然后使用计数器检查该数字是否重复,当它完成时将数字添加到新列表中,这样您就有一个已经检查过的数字列表,您可以利用 例如: 你有一个这样的列表[1,1,5,1,7,9,9] 首先,您将选择数字 1 并检查列表中的每个元素是否等于 1,如果是,则增加您的计数器。 完成后,您必须将数字 1 添加到一个新列表中,我们将其称为“alreadyChecked”,仅用于示例,然后我们必须有一个变量作为最小值,另一个变量作为最大值,并检查计数器是否大于最大值如果它低于最小值并正确分配。 现在对于下一个数字 1,您将检查该数字是否已经存在于 alreadyChecked 列表中,如果是,您只需转到下一个数字,依此类推。

【讨论】:

  • 这与我使用数组计算随机化每个数字的频率的提议大致相同,只是更复杂。
【解决方案2】:

为什么没有函数?将结果存储在一个表中并使用 SQL count() 和 group by 函数来对任何给定数字的每次出现进行计数。如果您按降序排序,您的最高计数将首先出现。

【讨论】:

    猜你喜欢
    • 2020-12-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-20
    • 1970-01-01
    • 2020-01-26
    • 1970-01-01
    相关资源
    最近更新 更多