【发布时间】:2020-07-12 20:24:10
【问题描述】:
我正在尝试解决pseint伪代码程序中的算法问题,问题如下:
如何计算姓名列表中每个姓名的重复次数?有谁知道如何做到这一点?
我知道如何为 1 个单一值执行此操作(只要我知道),但我无法确定如何将其调整为我正在寻找的内容
【问题讨论】:
-
你能添加你知道的1个单值的方法吗?因为你正在寻找适应它。
标签: algorithm logic pseudocode
我正在尝试解决pseint伪代码程序中的算法问题,问题如下:
如何计算姓名列表中每个姓名的重复次数?有谁知道如何做到这一点?
我知道如何为 1 个单一值执行此操作(只要我知道),但我无法确定如何将其调整为我正在寻找的内容
【问题讨论】:
标签: algorithm logic pseudocode
DIVIDE ET IMPERA 你的问题 :)
在你的列表中找到不同的名字
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;
你说你已经知道如何计算一个元素在数组中的次数,所以剩下的交给你,你只需要为所有不同的值做这件事。
【讨论】:
对于如何计算一个唯一名称出现在重复名称的列中的次数的问题,我无法遵循之前的任何一个答案。
[尽管这与 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 这取决于在简单查询设计中添加一列。
它确实对我有用。
这是原帖的截图:
【讨论】:
好吧,我对此有一个想法。
首先你获取一个列表元素并迭代列表搜索一个相等的元素
第二个元素匹配然后计数,当到达列表的末尾,所以删除这个元素并重新开始
所以最后你将计算列表中的所有元素。
【讨论】: