我想在 arr2 数组中存储最大和最小频率的元素?但不能够

发布于 2025-01-10 07:22:23 字数 2504 浏览 0 评论 0原文

我想在 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;
    }

I want to store elements of maximum and minimum frequency in the arr2 array if there are more than one element of same frequency then both the elements should be stored ? But it is showing wrong results and i am not able to find what is the err. Can anyone help me with this. Any kind of help would be greatly appreciated.

 #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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

暮色兮凉城 2025-01-17 07:22:23

正如 @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\n", count[i], arr[i]);
        }
        if (count[i] == max) {
                printf("max frequency %d value %d\n", 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

As @SparKot suggests, sorting the array makes the problem simple. Would you please try:

#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\n", count[i], arr[i]);
        }
        if (count[i] == max) {
                printf("max frequency %d value %d\n", count[i], arr[i]);
        }
    }

    return 0;
}

Sample input (n=10):

6
1
2
5
1
2
3
1
3
6

Output:

max frequency 3 value 1
min frequency 1 value 5
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文