计算数组-C中重复元素的数量

发布于 2025-02-08 21:24:50 字数 756 浏览 4 评论 0原文

从一个大小n的数组中获取用户的输入,并打印重复元素的总数(发生两次或更多次的元素)。

输入格式:第一行包含N。第二行包含由空间隔开的N正整数。

输出格式:重复元素的计数。

我编写的程序仅针对两个相同的元素,并且未能读取超过2个重复。

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

int main()
{
    int arr[1000],i,j,n,count=0;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]==arr[j])
            {
                count=count+1;
                break;
            }
        }
    }
    printf("%d",count);

}

输入(对于八位数的示例):

1 2 3 1 2 1 5 6

此代码的输出不正确:

3

预期输出为2,具有重复项的元素计数(1具有两个重复项,2个重复)。

所以请解释一下在做什么错?

Take a input from user in an Array of a size N and print the total number of duplicate elements (The elements which occur two or more times).

Input Format: The first line contains N. The second line contains the N positive integer separated by a space.

Output Format: Count of duplicate elements.

The program I wrote works for only two same elements and fails to read more than 2 duplicates.

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

int main()
{
    int arr[1000],i,j,n,count=0;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]==arr[j])
            {
                count=count+1;
                break;
            }
        }
    }
    printf("%d",count);

}

Input (for an eight-digit example):

1 2 3 1 2 1 5 6

Incorrect output of this code:

3

The expected output is 2, the count of elements which has duplicates (1 has two duplicates and 2 has one duplicate).

So please explain what am doing wrong?

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

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

发布评论

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

评论(4

顾铮苏瑾 2025-02-15 21:24:50

您可以使用以下代码进行相同的代码。它首先分为数组:

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

int main()
{
    int arr[1000],i,j,n,count=0, min;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++)
    {
        min = i;
        for(j=i+1;j<n;j++)
        {
            if(arr[min]>arr[j])
            {
                min = j;    
            }
        }
        {
            int temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;
        }
    }

    for(i=1;i<n;i++)
    {
        if(arr[i]==arr[i-1])
        {
            count++;
            while(arr[i]==arr[i-1]) i++;
        }
    }
    printf("%d",count);

    return 0;
}

您可以在这里找到它

You can use following code for the same. It first sorts the array:

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

int main()
{
    int arr[1000],i,j,n,count=0, min;
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++)
    {
        min = i;
        for(j=i+1;j<n;j++)
        {
            if(arr[min]>arr[j])
            {
                min = j;    
            }
        }
        {
            int temp = arr[min];
            arr[min] = arr[i];
            arr[i] = temp;
        }
    }

    for(i=1;i<n;i++)
    {
        if(arr[i]==arr[i-1])
        {
            count++;
            while(arr[i]==arr[i-1]) i++;
        }
    }
    printf("%d",count);

    return 0;
}

You can find it working here

美人骨 2025-02-15 21:24:50

i == 0&amp;&amp; j == 4i == 0&amp; j == 6再次增加时,您的计数会增加。 4&amp; j == 6 。当第三个检查是多余的。因此,要解决i&lt; j条件,并休息以停止检查已经检查的元素。

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

int main()
{
    int arr[1000],i,j,n,count=0;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(arr[i]==arr[j]&& i!=j){
               if(i<j){
                    count=count+1;
                    break;
                }
                else{
                    break;
                }
            }
        }
    }
    printf("%d",count);
}

Your count increase when i==0&&j==4, i==0&&j==6 and again at i==4&j==6. When the third check is redundant. So to resolve that put in a i<j condition and break to stop checking the already checked elements.

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

int main()
{
    int arr[1000],i,j,n,count=0;
    scanf("%d",&n);

    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }

    for(i=0;i<n;i++){
        for(j=0;j<n;j++){
            if(arr[i]==arr[j]&& i!=j){
               if(i<j){
                    count=count+1;
                    break;
                }
                else{
                    break;
                }
            }
        }
    }
    printf("%d",count);
}
老街孤人 2025-02-15 21:24:50

以下Java程序花费的时间更少,并且非常灵活,请根据X数组中的数字增加Y数组的大小。

public void countDuplicates() {
    int[] x = {1,2,3,4,5,9,1,0,9,1,1,2,4};
    int[] y = new int[10];
    for(int i =0 ; i< x.length ; i++) {
        y[x[i]] = y[x[i]] + 1;
    }
    int c = 0;
    for (int a:y) {
        System.out.println(c+"---> "+a);
        c++;
    }
}

The below java program takes less time and it is very flexible, increase the y array size based on the number in x array.

public void countDuplicates() {
    int[] x = {1,2,3,4,5,9,1,0,9,1,1,2,4};
    int[] y = new int[10];
    for(int i =0 ; i< x.length ; i++) {
        y[x[i]] = y[x[i]] + 1;
    }
    int c = 0;
    for (int a:y) {
        System.out.println(c+"---> "+a);
        c++;
    }
}
肤浅与狂妄 2025-02-15 21:24:50

您可以使用此代码,它将编写-1代替元素的所有重复项,因此它将计算仅具有重复项的元素。

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cout << "Enter The Size of the Array ";
    cin >> n;
    int *a = new int[n];
    cout << "Enter The Elements of the Array\n";
    for(int i = 0; i < n; i++) cin >> a[i];
    int count = 0;
    bool duplicate = false;
    for(int i = 0; i < n; i++){
        duplicate = false;
        if(a[i] == -1) continue;
        for(int j = i+1; j < n; j++){
            if(a[j] == a[i]) {
                duplicate = true;
                a[j] = -1;
            }
        }
        if(duplicate) count++;
    }
    cout << count ;
    return 0;
}

You can use this code, It will write -1 in place of all the duplicates of an element and hence it will count the elements having duplicates only once.

#include<bits/stdc++.h>
using namespace std;

int main()
{
    int n;
    cout << "Enter The Size of the Array ";
    cin >> n;
    int *a = new int[n];
    cout << "Enter The Elements of the Array\n";
    for(int i = 0; i < n; i++) cin >> a[i];
    int count = 0;
    bool duplicate = false;
    for(int i = 0; i < n; i++){
        duplicate = false;
        if(a[i] == -1) continue;
        for(int j = i+1; j < n; j++){
            if(a[j] == a[i]) {
                duplicate = true;
                a[j] = -1;
            }
        }
        if(duplicate) count++;
    }
    cout << count ;
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文