【问题标题】:two arrays, find the missing numbers两个数组,找到缺失的数字
【发布时间】:2022-01-16 23:28:27
【问题描述】:

给定两个数组,第一个有“n”个数字,第二个有“n-m”个数字;第二个数组与第一个数组的顺序不同。如果有多个具有相同值的数字,则它们以原始数组中的位置顺序结束。此外,第二个数组中的所有值也在第一个数组中找到。我必须按照它们在第一个数组中出现的顺序来查找“m”缺失的数字。

input:
7 3
12 34 45 29 100 87 32
100 87 12 34

output: 
45 29 32
#include <iostream>
using namespace std;

int main()
{
    int n, missing_number = 0, m, i, j, v[1201], w[1201];
    cin >> n >> m;
    for (i = 0; i < n; ++i) {
        cin >> v[i];
    }
    for (i = 0; i < n - m; ++i) {
        cin >> w[i];
    }
    for (i = 0; i < n; ++i) {
        missing_number = 1;
        for (j = 0; j < n - m; ++j) {
            if (v[i] == w[j]) {
                missing_number = -1;
            }
        }
        if (missing_number == 1) {
            cout << v[i] << " ";
        }
    }
    if (m == 0)
        cout << "there are no missing numbers";
    return 0;
}

我的代码不适用于重复数字,例如:

7 3 
2 6 1 9 3 2 4
4 1 2 3

我的输出应该在哪里:

6 9 2 

【问题讨论】:

  • 请同时包含您的代码产生的错误输出。
  • ...另外,你有没有进行任何错误搜索?如果您有任何信息,我们可以使用该信息...
  • 你有没有一次输入一个数字来调试你的算法?
  • 有了std::unordered_map,你可以把你的O(n²)算法变成O(n)

标签: c++ arrays for-loop numbers


【解决方案1】:

您的程序似乎正在输出正确的结果。但是,我觉得我需要重构您的代码以提高其可读性并删除其中使用的不良做法。

以下与您的代码相同,但有一些改进:

#include <iostream>
#include <array>
#include <limits>


int main( )
{
    std::array<int, 1201> arr1; // use std::array instead of raw arrays
    std::array<int, 1201> arr2;

    std::size_t arr1_size { }; // renamed n
    std::size_t arr2_size { }; // renamed m

    std::cin >> arr1_size >> arr2_size;

    if ( arr2_size == 0 ) // this if statement should be here to help end
    {                     // the program early on to prevent the execution
                          // of the for-loops
        std::cout << "There are no missing numbers.\n";
        return 0;
    }

    for ( std::size_t idx { }; idx < arr1_size; ++idx ) // use std::size_t
    {                                                   // for the loop counters
        std::cin >> arr1[ idx ];
    }

    for ( std::size_t idx { }; idx < arr1_size - arr2_size; ++idx )
    {
        std::cin >> arr2[ idx ];
    }

    for ( std::size_t arr1_idx { }; arr1_idx < arr1_size; ++arr1_idx )
    {
        bool isNumberMissing { true }; // this should be of type bool

        for ( std::size_t arr2_idx { }; arr2_idx < arr1_size - arr2_size; ++arr2_idx )
        {
            if ( arr1[ arr1_idx ] == arr2[ arr2_idx ] )
            {
                isNumberMissing = false;

                // this is my trick for solving your code's bug
                arr2[ arr2_idx ] = std::numeric_limits<int>::min( );

                break; // break here to improve performance
            }
        }

        if ( isNumberMissing )
        {
            std::cout << arr1[ arr1_idx ] << " ";
        }
    }

    std::cout << '\n';
}

示例输入/输出#1:

7 3
12 34 45 29 100 87 32
100 87 12 34
45 29 32

示例输入/输出#2:

7 3
2 6 1 9 3 2 4
4 1 2 3
6 9 2

注意:见Why is "using namespace std;" considered bad practice?

【讨论】:

  • 在某处,输出中应该有一些换行符——例如,在“没有丢失的数字”消息之后,也可能在其他地方也有。我不相信first_array_elem_count 是n 的好交易,特别是当你有arr1 而不是array_1 或类似的东西时。也许arr1_size 是一个合理的妥协,与arr1_idx 平行。当你知道它们需要的大小时,你能不定义数组吗?
  • @Jonathan Leffler 我更新了答案。
  • 好。代码是否处理 OP 用重复值描述的问题?当我输入 7 3 / 2 6 1 9 3 2 4 / 4 1 2 3 时,我得到了 OP 所需的输出 6 9 而不是 6 9 2,所以我认为仍然缺少“重复数字”逻辑。
  • @Jonathan Leffle 解决了。
  • 如果std::numeric_limits&lt;int&gt;::min() 出现在输入列表中,则不起作用。
猜你喜欢
  • 1970-01-01
  • 2013-11-30
  • 2021-12-24
  • 2012-12-03
  • 1970-01-01
  • 2022-12-07
  • 1970-01-01
  • 1970-01-01
  • 2022-12-03
相关资源
最近更新 更多