小编use*_*oom的帖子

反转数组而不改变零的位置

我刚刚和我的朋友一起尝试了一些数据结构问题。我从一位朋友那里遇到了这个问题,他也无法解决。

问题:反转数组而不改变零的位置。示例:如果数组有 0 5 7 8 0 9 那么结果应该是 0 9 8 7 0 5。

我尝试过,但它在所有情况下都不能正确执行,如果代码看起来很难看,我很抱歉我现在是新手。

#include<iostream>
using namespace std;
int main()
{
    int arr[100], tot, i, j, temp;
    cout<<"Enter the Size for Array: ";
    cin>>tot;
    cout<<"Enter "<<tot<<" Array Elements: ";
    for(i=0; i<tot; i++)
        cin>>arr[i];
    cout<<"\nThe Original Array is:\n";
    for(i=0; i<tot; i++)
        cout<<arr[i]<<" ";
    j = tot-1;
    for(i=0; i<j; i++, j--)
    {
        if(arr[i] == 0) {
            i++;
            continue;
        }else if(arr[j] == 0) {
           j--;
           continue;
        }
        else {
            temp = …
Run Code Online (Sandbox Code Playgroud)

c++ arrays algorithm data-structures

2
推荐指数
1
解决办法
224
查看次数

标签 统计

algorithm ×1

arrays ×1

c++ ×1

data-structures ×1