【问题标题】:How can I count how many times each of the names in a names list is repeated?如何计算名称列表中每个名称重复的次数?
【发布时间】:2020-07-12 20:24:10
【问题描述】:

我正在尝试解决pseint伪代码程序中的算法问题,问题如下:

如何计算姓名列表中每个姓名的重复次数?有谁知道如何做到这一点?

我知道如何为 1 个单一值执行此操作(只要我知道),但我无法确定如何将其调整为我正在寻找的内容

【问题讨论】:

  • 你能添加你知道的1个单值的方法吗?因为你正在寻找适应它。

标签: algorithm logic pseudocode


【解决方案1】:

DIVIDE ET IMPERA 你的问题 :)

  1. 在你的列表中找到不同的名字

  2. foreach 上一步的元素,计算它在你的列表中出现了多少次。

在您的列表中找到不同的名称:

//create a new array and suppose it has as many elements as the given input:
let ris = new array[input.length]

//define an index for this array.
let k = 0
//in the rest of the algorithm let always k be the actual length of ris and the next possible index to use

//let's start iterating over the input list
for (i = 0; i<input.length; i++) {
    //let's check if input[i] it's already present in ris
    //we can look just for the first k elements
    let addCurrentName = true
    for (j = 0; j<k; j++) {
        if (input[i] == ris[j]) addCurrentName = false;
    }
    if (addCurrentName) {
         //we go inside this if only if in the previous search we found nothing
         ris[k] = input[i];
         k++;
    }
}

//now the first k positions of ris contain the distinct values of input

let ris2 = new array[k] //now we are sizing the result exactly
for (i = 0; i<k; i++) {
    ris2[i] = ris[i] //we are just creating the result of the right dimension and copying values into it
}

return ris2;

你说你已经知道如何计算一个元素在数组中的次数,所以剩下的交给你,你只需要为所有不同的值做这件事。

【讨论】:

  • 谢谢,这个例子帮助我更好地理解我需要什么
【解决方案2】:

对于如何计算一个唯一名称出现在重复名称的列中的次数的问题,我无法遵循之前的任何一个答案。

[尽管这与 OP 最初陈述的问题大致相同,但机器人已将此标记为不清楚。所以要清楚,例如,你有一个列:

约翰

杰克

玛丽

杰克

约翰

约翰

约翰

你想返回一个结果:

约翰 4

杰克 2

3 月 1 日

这就是“计算一个唯一名称在重复名称的列中出现的次数”的含义。每个名称后跟它出现的频率。我希望机器人这次能得到它。]

我在这里找到了比上述更简单的解决方案: https://community.spiceworks.com/topic/605190-count-the-number-of-times-a-value-appears-in-a-column-using-ms-access 这取决于在简单查询设计中添加一列。

它确实对我有用。

这是原帖的截图:

【讨论】:

  • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
【解决方案3】:

好吧,我对此有一个想法。

首先你获取一个列表元素并迭代列表搜索一个相等的元素

第二个元素匹配然后计数,当到达列表的末尾,所以删除这个元素并重新开始

所以最后你将计算列表中的所有元素。

【讨论】:

  • 非常感谢,你帮助我更好的理解,最后我走的是根据初始列表(ArrayA)中哪些元素不同的搜索来创建数组(ArrayB)的路径,并且稍后在数组(ArrayB)中分配另一行,我在其中分配重复次数的值
猜你喜欢
  • 2021-05-05
  • 1970-01-01
  • 1970-01-01
  • 2018-02-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多