【问题标题】:C++ How to the total number of duplicate elements in the array?C++如何计算数组中重复元素的总数?
【发布时间】:2021-06-05 20:03:45
【问题描述】:

我正在努力完成我的作业,但我在重复元素项目中遇到了一些问题。我试图找出问题出在哪里,但我找不到。我的代码在序列很小的时候可以正常工作,但是当序列很大时就会出现问题。

在这个序列中,我期望得到图片中的数字,但我总是得到第二张图片中的数字。可以看到,重复元素中的这一项总是有问题。

#include <iostream>

using namespace std;

int main()
{
    int count = 0;
    int arrayA[25];
    int arrayB[25];
    int min,max;
    int num;
    int i = 0;
    int n = 0;
    int temp = 0;
    int sum = 0;

    //Input
    cout << "Input the number of elements to store in the array: ";
    cin >> n;
    cout << n << endl;
    cout << "Input "<< n <<" integers:" << endl;
    //Store arrayA
    if(count < n){
        for(i = 0; i < n; i++){
            cin >> num;
            cout << "integer - " << count;
            cout << " : " << num <<endl;
            arrayA[count] = num;
            count++;
        }
    }
    //store arrayB
    for(i = 0;i < count; i++){
        arrayB[i] = arrayA[count - i - 1];
    }

    //Forwards Array
    cout << "The values stored into the array are :" << endl;
    for(i = 0;i < count; i++){
        cout << arrayA[i] << " ";
    }
    cout << endl;

    //Backwards Array
    cout << "The values stored into the array in reverse are :" << endl;
    for(i = 0; i < count; i++){
        cout << arrayB[i] <<" ";
    }
    cout << endl;

    //Sum
    for(i = 0; i < count; i++){
        sum += arrayA[i];
    }

    //Max & Min
    for(i = 0; i < count;i++){
        if(max < arrayA[i]){
            max = arrayA[i];
        }
    }
    for(i = 0; i < count; i++){
        if(min > arrayA[i]){
            min = arrayA[i];
        }
        if(arrayA[i]== 0){
            min = 0;
            break;
        }
    }

    //Duplicate elements
    count = 0;
    for(i = 0; i < n; i++){
        for(temp = i + 1; temp < n; temp++){
            if(arrayA[i] == arrayA[temp] ){
                count++;
                break;
            }
        }
    }

    cout << "The sum of all elements of the array is ";
    cout << " " << sum << endl;
    cout << "The total number of duplicate elements in the array is ";
    cout << count << endl;
    cout << "The maximum and minimum element in the array are ";
    cout << min << " , " << max;

    return 0;
}

【问题讨论】:

  • 您的输出应该包含为文本,而不是图像。您是否考虑过如果一个数字出现两次以上会发生什么(例如您的示例中的“4”)?
  • 在计数时打印您要比较的项目,一个简单的调试步骤,就会给您很多洞察力。

标签: c++ arrays loops


【解决方案1】:

您应该尝试通过打印进行更多调试,以了解发生了什么。您并没有真正考虑到一个数字可以重复多次,并且根据点差,它会计算多次。原因如下:

//Duplicate elements
count = 0;
for(i = 0; i < n; i++){
    for(temp = i + 1; temp < n; temp++){
        //cout << "Comparing "<<  arrayA[i] << " to "<< arrayA[temp]<<endl; 
        if(arrayA[i] == arrayA[temp] ){
            cout << "ITEM DUPLICATED at i , "<<i<< " item "<< arrayA[i]<< " and temp "<< temp<< " item "<< arrayA[temp] << endl;
            count++;
            break;
        }
    }
}

                                                                                                         

项目在 i 重复,0 项目 4 和临时 7 项目 4
项目在 i 重复,2 项目 -2 和临时 10 项目 -2
项目在 i 重复,4 项目 -3 和临时 14 项目 -3
项目在 i 重复,5 项目 3 和临时 11 项目 3
项目在 i 重复,7 项目 4 和临时 8 项目 4
项目在 i 重复,12 项目 -1 和临时 13 项目 -1

如您所见,有 4 - 4 两次。如果你在纸上做的话很容易看到(虽然你不应该)

4A -4B -2C 1D -3E 3F 5G 4H 4I 0J -2K 3L -1M -1N -3O

4a -4b -2c 1d -3e 3f 5g 4h 4i 0j -2k 3l -1m -1n -3o

已建立连接:

4A 4h

-2C -2k

-3E -3o

3F 3l

4H 4i

因此,从技术上讲,代码正在执行您要求它执行的操作。如果您想唯一计数(如果 4 出现 15 次,则将其视为仅重复 4),您应该检查该数字是否已被检查。使用矢量或地图(C++ 中的字典)可以让您做到这一点!或者带有列表的列表(元素,出现次数)

【讨论】:

    猜你喜欢
    • 2021-07-19
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 2010-10-08
    • 1970-01-01
    • 2020-09-17
    • 1970-01-01
    相关资源
    最近更新 更多