c++ 输入包含多个零元素的数组,请将所有连续零组替换为单个零

nx7onnlm  于 2023-01-22  发布在  其他
关注(0)|答案(2)|浏览(112)
#include <iostream> 
using namespace std;

int main()
{
    int size = 0, new_size=0, zero_streak = 0;

    cout << "Input length of an array: ";
    cin >> size;

    double *arr = new double[size];
    double* arr2{ new double[size] };

    cout << "\nInput elements of an array:\n";
    for (int index = 0; index < size; index++)
    {
        cin >> arr[index];   
    }
    
    for (int index = 0; index < size; index++)
    {
        if (arr[index] == 0) {
            ++zero_streak;
            if (zero_streak == 1 || zero_streak == 0)
            {
                arr2[index]=arr[index];
                ++new_size;
            }                
        }
        else if (arr[index] != 0)
        {
            arr2[index] = arr[index];
            ++new_size;
            zero_streak = 0;
        }            
    }

    cout << "\nNew array looks like this:\n";
    for (int index = 0; index < new_size; index++)
        cout << arr2[index] << " ";
    
    delete[] arr;
    delete[] arr2;

    return 0;
}

所有的结构看起来都没问题,但问题是当元素被复制到新数组时,第一个零后面的其他连续零会抛出未知值。
此外,第一个数组的最后一个元素不会复制到第二个数组。
有什么建议可以解决这个问题吗?

wgeznvg7

wgeznvg71#

我们对arr和arr2都使用了index,但是需要一个单独的index来跟踪arr2中的新位置,因为得到的字符串长度是不同的:下面是经过修正和简化的for循环:

for (int index = 0; index < size; index++)
{
    if (arr[index] == 0) {
        ++zero_streak;
    }
    else if (arr[index] != 0)
    {
        // Write any zero streaks
        if (zero_streak>0)
        {
            arr2[new_size] = 0;
            ++new_size;
            zero_streak = 0;
        }
           
        arr2[new_size] = arr[index];
        ++new_size;
        zero_streak = 0;
    }
}

// And catch any trailing 0 streaks here
// For example 0 0 1 [0 0]
if (zero_streak > 0)
{
    arr2[new_size] = 0;
    ++new_size;
    zero_streak = 0;
}

这意味着“1 0 0 0 1”的输入变为“1 0 1”,“0 0 1 0 0”变为“0 1 0”

t2a7ltrp

t2a7ltrp2#

你似乎具备了一切必要的要素,让我们来看看其中的逻辑。

for (int index = 0; index < size; index++)
{
    if (arr[index] == 0) {

        // what happens when zero_streak increments to 2 and above ?
        /*             
        ++zero_streak;
        if (zero_streak == 1 || zero_streak == 0)  // why == 1 ?
        */

        // should read more like this: 

        if (zero_streak == 0)
        {
            zero_streak = 1;

            // why arr2[index]?  index is the index of the symbol in input array
            /*
                arr2[index] = arr[index];
                ++new_size;
            */

            // You want to insert into the last position of the output 
            // array, then increase the size.
            arr2[new_size++] = arr[index];
        }
            
    }
    else // if (arr[index] != 0)  unnecessary.
    {
        // Same problem as avove
        /*
            arr2[index] = arr[index];
            ++new_size;
        */
        zero_streak = 0;
        // You want to insert into the last position of the output 
        // array, the increase the size.
        arr2[new_size++] = arr[index];
    }            
}

这可以更简洁地写成:

for (int index = 0; index < size; index++)
{
    if (arr[index] == 0) {
        if (zero_streak)
            continue;      // jump back to top of loop.
        zero_streak = 1;
    }
    else {
        zero_streak = 0;
    }

    arr2[new_size++] = arr[index];
}

您可以在这里使用代码:https://godbolt.org/z/qqz7z89bq

相关问题