我想要在arr2数组中存储最大和最小频率的元素?但却不能 [英] I want to store elements of maximum and minimum frequency in the arr2 array ? But not able to

查看:0
本文介绍了我想要在arr2数组中存储最大和最小频率的元素?但却不能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在arr2数组中存储最大和最小频率的元素如果有多个相同频率的元素,那么这两个元素都应该存储?但它显示的结果是错误的,我找不到错误是什么。有人能帮我这个忙吗。任何形式的帮助都将不胜感激。

 #include <stdio.h>
    int main()
    {
        int n;
        scanf("%d", &n);
        int arr[n];
        for (int i = 0; i < n; i++)
        {
            scanf("%d", &arr[i]);
        }
        int arr2[n];
        int prevcount = 0;
        int k = 0;
        // for finding max element
        for (int i = 0; i < n; i++)
        {
            int count = 0;
            //counting the number of times it has occured
            for (int j = 0; j < n; j++)
            {
                if (arr[i] == arr[j])
                {
                    count++;
                }
            }
            // checking if the same element was not there in the new array
            for (int i = 0; i < k; i++)
            {
                if (arr[i] == arr[k])
                {
                    goto nextit;
                }
            }
            //it will update the kth element if the count is greater than the prev count
            if (prevcount < count)
            {
                arr2[k] = arr[i];
            }
            //if these both are same but the number is different  then will iterate k by 1 and store that element as well
            else if (prevcount == count)
            {
                k++;
                arr2[k] = arr[i];
            }
            prevcount = count;
        nextit:
        }
        // for finding min element
        prevcount = 1000;
        for (int i = 0; i < n; i++)
        {
            int count = 0;
            for (int j = 0; j < n; j++)
            {
                if (arr[i] == arr[j])
                {
                    count++;
                }
            }
            // checking if the same element was not there in the new array if there is then go to the next iteration
            for (int i = 0; i < k; i++)
            {
                if (arr[i] == arr[k])
                {
                    goto nextit2;
                }
            }
            if (prevcount > count)
            {
                arr2[k] = arr[i];
            }
            else if (prevcount == count)
            {
                k++;
                arr2[k] = arr[i];
            }
            prevcount = count;
        nextit2:
        }
    
        for (int i = 0; i < k; i++)
        {
            printf("%d ", arr2[i]);
        }
        return 0;
    }

推荐答案

正如@SparKot建议的那样,对数组进行排序可以使问题变得简单。请您试一试:

#include <stdio.h>
#include <stdlib.h>

// compare values numerically
int numeric(const void *a, const void *b)
{
    return (*(int *)a < *(int *)b) ? -1 : (*(int *)a > *(int *)b);
}

int main()
{
    int n, i, j;
    int *arr;                   // input array
    int *count;                 // count frequency: initialized to 0's by calloc
    int min = 0;                // minimum occurrences
    int max = 0;                // maximum occurrences

    scanf("%d", &n);
    if (NULL == (arr = malloc(n * sizeof(int)))) {
        perror("malloc");
        exit(1);
    }
    if (NULL == (count = calloc(n, sizeof(int)))) {
        perror("calloc");
        exit(1);
    }
    for (i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    qsort(arr, n, sizeof(int), numeric);

    // count the length of sequence of the same numbers
    for (i = 0; i < n; i++) {
        for (j = 0; i + j < n && arr[i] == arr[i + j]; j++) {
            ;
        }
        count[i] = j;           // i'th element has length j
        i += j - 1;             // jump to next number
    }

    // find minimum and maximum frequencies
    for (i = 0; i < n; i++) {
        if (count[i]) {
            if (min == 0 || count[i] < min) min = count[i];
            if (max == 0 || count[i] > max) max = count[i];
        }
    }

    // report the result
    for (i = 0; i < n; i++) {
        if (count[i] == min) {
                printf("min frequency %d value %d
", count[i], arr[i]);
        }
        if (count[i] == max) {
                printf("max frequency %d value %d
", count[i], arr[i]);
        }
    }

    return 0;
}

样本输入(n=10):

6
1
2
5
1
2
3
1
3
6

输出:

max frequency 3 value 1
min frequency 1 value 5

这篇关于我想要在arr2数组中存储最大和最小频率的元素?但却不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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