c从整数列表获取方式 [英] C get mode from list of integers

查看:116
本文介绍了c从整数列表获取方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要编写一个程序来查找模式。还是最发生的整数或整数。
因此,

I need to write a program to find the mode. Or the most occurrence of an integer or integers. So,

1,2,3,4,1,10,4,23,12,4,1将具有图1和4模式

1,2,3,4,1,10,4,23,12,4,1 would have mode of 1 and 4.

我真的不知道我应该用什么样的算法。我有一个困难时期试图想的东西会工作。

I'm not really sure what kind of algorithm i should use. I'm having a hard time trying to think of something that would work.

我在想一些频率表的排序,也许在那里我可以通过阵列,然后再通过,也许创建一个链表。如果链接的不包含该值将其添加到链接,如果它不再加入1的值。

I was thinking of a frequency table of some sort maybe where i could go through array and then go through and create a linked list maybe. If the linked doesn't contain that value add it to the linked, if it does then add 1 to the value.

所以,如果我从上面有同样的事情。通过循环
1,2,3,4,1,10,4,23,12,4,1

So if i had the same thing from above. loop through 1,2,3,4,1,10,4,23,12,4,1

然后列表是空的,所以用加号= 1和值= 1节点。
2犯规存在所以用数= 2和值= 1等添加节点。
获取对1和1已经存在那么值= 2了。

Then list is empty so add node with number = 1 and value = 1. 2 doesnt exist so add node with number = 2 and value = 1 and so on. Get to the 1 and 1 already exists so value = 2 now.

我想通过数组必须循环,然后遍历链表每次发现的价值。

I would have to loop through the array and then loop through linked list everytime to find that value.

在我正在做的,然后经过链表,创建一个新的链接列表,将持有的模式。所以,我的头设置为是1。然后我通过包含OCCURENCES链表和比较价值的第一要素。如果当前节点的OCCURENCES是>当前最高的,然后我头设置到此节点。如果=到最高,然后我的节点添加到模式链表。

Once i am done then go through the linked list and create a new linked list that will hold the modes. So i set the head to the first element which is 1. Then i go through the linked list that contains the occurences and compare the values. If the occurences of the current node is > the current highest then i set the head to this node. If its = to the highest then i add the node to the mode linked list.

在我正在做的通过模式列表i循环和打印值。

Once i am done i loop through the mode list and print the values.

不知道这是否会工作。有谁看到什么错呢?是否有更简单的方法来做到这一点?我在想一个哈希表太多,但真的不知道该怎么做,在C

Not sure if this would work. Does anyone see anything wrong with this? Is there an easier way to do this? I was thinking a hash table too, but not really sure how to do that in C.

感谢。

推荐答案

您的算法是罚款家庭作业。有各种东西,你可以做优化code,如:

The algorithm you have is fine for a homework assignment. There are all sorts of things you could do to optimise the code, such as:


  • 使用二叉树求效益,以

  • 使用计数的数组,其中指数是多少(假设编号范围是有限的)。

不过,我想你会发现他们没有必要在这种情况下。对于作业,目的只是为了表明您了解如何编写程序,不在于你知道出榨取性能的最后盎司的各种技巧。您教育家将寻找更易读的,层次分明,code比棘手的优化。

But I think you'll find they're not necessary in this case. For homework, the intent is just to show that you understand how to program, not that you know all sorts of tricks for wringing out the last ounce of performance. Your educator will be looking far more for readable, structured, code than tricky optimisations.

我将在下面说明什么,我会怎么做。你显然可以自由尽可能多的使用我的建议还是很少,你想,这取决于你想要多少满意度来获得你自己做。我将只提供伪code,这是我的家庭作业问题的标准做法。

I'll describe below what I'd do. You're obviously free to use my advice as much or as little as you wish, depending on how much satisfaction you want to gain at doing it yourself. I'll provide pseudo-code only, which is my standard practice for homework questions.

我将开始与结构拿着数,计数和下一个指针(您链表)和全局指针到第一个:

I would start with a structure holding a number, a count and next pointer (for your linked list) and the global pointer to the first one:

typedef struct sElement {
    int number;
    int count;
    struct sElement *next;
} tElement;
tElement first = NULL;

然后创建和使用列表中创建一些功能:

Then create some functions for creating and using the list:

tElement *incrementElement (int number);
tElement *getMaxCountElement (void);
tElement *getNextMatching (tElement *ptr, int count);

这些功能将分别为:

Those functions will, respectively:


  • 增量元素计数(或创建并设置数为1)。

  • 扫描所有返回的最大计数的元素。

  • 获取如果没有更多的下一个元素的指针相匹配的数量,开始在一个给定的点,或NULL。

伪code每个:

def incrementElement (number):
    # Find matching number in list or NULL.

    set ptr to first
    while ptr is not NULL:
        if ptr->number is equal to number:
            return ptr
        set ptr to ptr->next

    # If not found, add one at start with zero count.

    if ptr is NULL:
        set ptr to newly allocated element
        set ptr->number to number
        set ptr->count to 0
        set ptr->next to first
        set first to ptr            

    # Increment count.

    set ptr->count to ptr->count + 1

 

def getMaxCountElement (number):
    # List empty, no mode.

    if first is NULL:
        return NULL

    # Assume first element is mode to start with.

    set retptr to first

    # Process all other elements.

    set ptr to first->next
    while ptr is not NULL:
        # Save new mode if you find one.

        if ptr->count is greater than retptr->count:
            set retptr to ptr
        set ptr to ptr->next

    # Return actual mode element pointer.

    return retptr

 

def getNextMatching (ptr, number):
    # Process all elements.

    while ptr is not NULL:
        # If match on count, return it.

        if ptr->number is equal to number:
            return ptr
        set ptr to ptr->next

    # Went through whole list with no match, return NULL.

    return NULL

然后你的主程序变为:

Then your main program becomes:

# Process all the numbers, adding to (or incrementing in) list .

for each n in numbers to process:
    incrementElement (n)

# Get the mode quantity, only look for modes if list was non-empty.

maxElem = getMaxCountElement ()
if maxElem is not NULL:
    # Find the first one, whil exists, print and find the next one.

    ptr = getNextMatching (first, maxElem->count)
    while ptr is not NULL:
        print ptr->number
        ptr = getNextMatching (ptr->next, maxElem->count)

这篇关于c从整数列表获取方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆