移位数组元素

新手上路,请多包涵

我需要一些帮助,我知道以前有人问过这个问题,但我不明白,也无法解决,所以我需要帮助。我需要将数组的元素移动到左侧的位置。因此,如果输入为 1,2,3,4,5,则输出为 2,3,4,5,1。我对右边做了同样的事情,但左边我想不通,还请解释一下逻辑,谢谢。

     #include <iostream>
    using namespace std;
    int a[100],n,i,tempr,templ;
    int main()
    {
    cin>>n;
    for(i=1;i<=n;i++) cin >> a[i];
    for(i=1;i<=n;i++)
        {
            tempr = a[n];
            a[n] = a[i];
            a[i] = tempr;
            cout<<"Right: "<<a[i]<<endl;
        }
    for(i=1;i<=n;i++)
        {
            templ = a[2];
            a[2] = a[i];
            a[i] = templ;
            cout<<"Left: "<<a[i]<<endl;
        }
    return 0;
}

请帮忙!

原文由 Melin Ven 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 524
2 个回答

第一个问题是索引错误:

 for(i=1;i<=n;i++) cin >> a[i]; //wrong logic, C++ indexing start from 0

正确做法:

 for(i=0;i<n;i++) //all your loops

第二个问题是移位元素的错误逻辑:更正的版本:

 //input example: 1 2 3 4 5
//to the left
int temp = a[0]; //remember first element
for(i=0;i<n-1;i++)
{
    a[i] = a[i+1]; //move all element to the left except first one
}
a[n-1] = temp; //assign remembered value to last element
//output: 2 3 4 5 1
cout << "To left: " << endl;
for(i=0;i<n;i++)
    cout << a[i] << endl;

//to the right
temp = a[n-1]; //remember last element
for(i=n-1;i>=0;i--)
{
    a[i+1] = a[i]; //move all element to the right except last one
}
a[0] = temp; //assign remembered value to first element
//output: 1 2 3 4 5 because elements are shifted back by right shift
cout << "To right: " << endl;
for(i=0;i<n;i++)
    cout << a[i] << endl;


编辑:

如何显示两个班次:

 #include <iostream>
    using namespace std;
    int to_left[5], to_right[5],n,i,tempr,templ;
    int main()
    {

    cout << "Input array size: ";
    cin >> n;

    for(i=0;i<n;i++)
    {
        cin >> to_left[i]; //read values to first array
        to_right[i]=to_left[i]; //then copy values to second one
    }

    //shift first array to left
    int temp = to_left[0];
    for(i=0;i<n-1;i++)
    {
        to_left[i] = to_left[i+1]; //move all element to the left except first one
    }
    to_left[n-1] = temp; //assign remembered value to last element
    //output: 2 3 4 5 1
    cout << "To left: " << endl;
    for(i=0;i<n;i++)
        cout << to_left[i] << endl;

    //shift second array to right
    temp = to_right[n-1]; //remember last element
    for(i=n-1;i>=0;i--)
    {
        to_right[i+1] = to_right[i]; //move all element to the right except last one
    }
    to_right[0] = temp; //assign remembered value to first element
    //output: 1 2 3 4 5 because elements are shifted back by right shift
    cout << "To right: " << endl;
    for(i=0;i<n;i++)
        cout << to_right[i] << endl;

    return 0;
}

请注意,您的代码看起来非常像 C 代码。在 C++ 中,您可以在任何代码段中声明变量,而不仅仅是在开头。在 C++ 中,您可以在 for 循环中声明变量,如下所示: for(int i=0; i<...) - 不需要全局变量 i

作为参考,这将是一个很好的 C++ 代码示例,可以满足您面临的问题:

 #include <iostream>
#include <vector>
int main()
{
    std::size_t n; //size_t is unsiged type used for various sizes of containers or types
    std::cout << "Input array size: ";
    std::cin >> n;

    std::vector<int> to_left(n), to_right(n); //two dynamic arrays containing integers, takin n as their size

    for(std::size_t i=0;i<to_left.size();++i) //use vector size(), instead of n, also ++i in considered better for loops that i++ (may be faster)
    {
        std::cin >> to_left[i];
        to_right[i]=to_left[i];
    }

    int temp = to_left[0]; //declare temp here, not at the begining of code
    for(std::size_t i=0;i<n-1;++i)
        to_left[i] = to_left[i+1];
    to_left[n-1] = temp;

    std::cout << "To left: " << std::endl;
    for(std::size_t i=0;i<n;++i)
        std::cout << to_left[i] << std::endl;

    temp = to_right[n-1]; //reuse temp
    for(int i=to_right.size()-1;i>=0;--i) //note int, not std::size_t, because size_t is always >=0, loop would never end.
        to_right[i+1] = to_right[i];
    to_right[0] = temp;

    std::cout << "To right: " << std::endl;
    for(std::size_t i=0;i<n;i++)
        std::cout << to_right[i] << std::endl;

    return 0;
}

这将是理想的 C++ 代码:

 #include <iostream>
#include <vector>
#include <algorithm>
int main()
{
    std::size_t n;
    std::cout << "Input array size: ";
    std::cin >> n;

    std::vector<int> to_left(n), to_right(n);

    for(std::size_t i=0;i<to_left.size();++i)
    {
        std::cin >> to_left[i];
        to_right[i]=to_left[i];
    }

    // rotate first array to the left
    std::rotate(to_left.begin(), to_left.begin() + 1, to_left.end());

    // rotate second array to right
    std::rotate(to_right.rbegin(), to_right.rbegin() + 1, to_right.rend());

    std::cout << "To left:" << std::endl;
    for(auto x : to_left) //C++11 feature, x iterates through container
        std::cout << x << std::endl;

    std::cout << "To right:" << std::endl;
    for(auto x : to_right)
        std::cout << x << std::endl;

    return 0;
}

原文由 xinaiz 发布,翻译遵循 CC BY-SA 3.0 许可协议

int*  leftShiftOneByOneWIthoutTemp(int arr[], int sz)
{

    for (int i=0 ;i < sz-1; i++)
    {
        arr[i] = arr[sz-1] + arr[i];
        arr[sz-1] = arr[i] - arr[sz-1] ;
        arr[i] = arr[i] - arr[sz-1] ;
        std::cout << "iter  "<< i << std::endl;
        printArray(arr,5);

    }
    std::cout << "final "<< std::endl;
    printArray(arr,5);

    return arr;
}

原文由 Manisha Sinha 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题