移位数组元素

时间:2016-09-17 11:36:47

标签: c++ arrays algorithm shift

我需要一些帮助,我知道之前曾问过这个问题,但我没有得到它,我无法解决,所以我需要帮助。我需要将阵列的元素移动到左边的位置。因此,如果输入为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;
}

请帮忙!

7 个答案:

答案 0 :(得分:3)

第一个问题是索引错误:

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;
}    

答案 1 :(得分:1)

在C ++中交换元素的最简单方法是使用std :: iter_swap()

因此,对于交换元素1和4的4个元素的数组,您将执行以下操作

int a[4];
std::iter_swap(a, a+3);

请注意,您还需要#include <algorithm>才能使其正常工作

函数的基本逻辑是你在内存中给出2个元素的位置,所以当数组的第一个元素也是它在内存中的位置时,你可以传递一个+ n,当n等于要交换的元素的n-1索引号

答案 2 :(得分:1)

或者你可以使用完全为这些目的投射的memmove(...),这里是你的样本:

#include <iostream> 
#include <cstring> 
using namespace std; 
//rotate Left
void r_left(int *a,int n) 
{ 
  int tmp=a[0];
  memmove(a,a+1,sizeof(int)*(n-1));
  a[n-1]=tmp;
} 
//rotate right
void r_right(int *a,int n) 
{ 
    int tmp=a[n-1];
    memmove(a+1,a,sizeof(int)*(n-1));
    a[0]=tmp;
 } 
void show(int *a,int n)
{
   while(n--)
   cout<<*a++<<' ';
   cout<<endl;
}

int main() 
{ 
   int ar[]={1,2,3,4,5};
   int n=sizeof(ar)/sizeof(ar[0]);
   r_left(ar,n);
   show(ar,n);
   r_right(ar,n);
   show(ar,n);
return 0;
}

答案 3 :(得分:0)

    #include <iostream>
    using namespace std;
    int a[100], outR[100], outL[100], n, i;
    int main() {
      cin >> n;
      for (i = 0; i < n; i++) cin >> a[i];
      // Right
      for (i = 0; i < n; i++) {
        outR[i+1]= a[i];
      } 
      outR[0] = a[n-1]; // add first number
      // Left
      for (i = 1; i < n; i++) {
        outL[i-1]= a[i];
      }         
      outL[n-1] = a[0]; // add last number
      // Answer
      cout << "Right:\n";
      for(i=0; i<n; i++) {
        cout << outR[i] << endl;
        }
      cout << "Left:\n";
      for(i = 0; i < n; i++) {
        cout << outL[i] << endl;
      }
        return 0;
    }

简单的答案,你可以很容易地看到一切,祝你好运。

你可能感兴趣,矢量编码“,如果你花一些时间在这上面似乎更容易:

    #include <iostream>
    #include <vector>
    using namespace std;
    vector <int> a, outR, outL;
    size_t i;
    int main () {
      int n, temp_int;
      cin >> n;
      while (n--) {
        cin >> temp_int;  // here you read number to your vector
        a.push_back(temp_int);  // here you add this to vector
        // remember that vector start from element 0 as like arrays
      }
      // Left
      // remember that last element will be first
      // you may have acces to size of your vector easily
      for (i = 0; i < (a.size()-1); i++) {
        outL.push_back(a.at(i+1));  // here you create new vector
      }
      outL.push_back(a.at(0)); // add last elemet which rotated

      // Right
      // to rotate left first you have push last element so
      outR.push_back(a.at(a.size()-1)); // add first elemet which rotated
      for (i = 1; i < a.size(); i++) {
        outR.push_back(a.at(i-1));  // here you push rest
        }

      cout << "Left"  << "\n";
      for (i = 0; i < a.size(); i++) {
        cout << outL.at(i) << endl;  // here you print value  
      }
      cout << "Right" << "\n";
      for (i = 0; i < a.size(); i++) {
        cout << outR.at(i) << endl;  // here you print value  
      }
      return 0;
    }

答案 4 :(得分:0)

正如其他人已经说过的那样,关于指数。在for循环中,如果你的停止条件是i&lt; = size,你几乎总是遇到麻烦,因为C ++中的数组是零索引的。

Black Moses alogrithm是最容易理解的(也许是最好的),我读了你的代码,好像你试图通过数组将数组的第一个值交换到最后一个位置。下面我试图找出这种方法。

#include <stdio.h>
#include <tchar.h>
#include <iostream>

void ShiftLeft(int* pArr, size_t length)
{
  for (size_t i = 1; i < length; i++)
  {
    int tmp = pArr[i - 1]; // Preserves the previous value
    pArr[i - 1] = pArr[i]; // Overwrites the previous position with the current value
    pArr[i] = tmp;         // Stores the previous value in the current position
                           // All in all the first value is swapped down the array until it is at the length - 1 position
                           // and all the other values are swapped to the left.

    /* For an array with 4 values the progression is as follows:
      i = 0: 1 2 3 4
      i = 1: 2 1 3 4
      i = 2: 2 3 1 4
      i = 3: 2 3 4 1    
    */

  }
}

void ShiftRight(int* pArr, size_t length)
{
  for (size_t i = length - 1; i > 0; i--)
  {
    // This code does exactly the same as for ShiftLeft but the loop is running backwards
    int tmp = pArr[i - 1];
    pArr[i - 1] = pArr[i];
    pArr[i] = tmp;
  }
}

void Print(int* pArr, size_t length)
{

  for (size_t i = 0; i < length; i++)
  {
    std::cout << pArr[i] << " ";
  }

  std::cout << std::endl;
}

int main()
{
  int arr[] = { 1, 2, 3, 4, 5, 6 };
  size_t length = sizeof(arr) / sizeof(arr[0]);

  Print(arr, length);
  ShiftLeft(arr, length);
  Print(arr, length);
  ShiftRight(arr, length);
  Print(arr, length);


  return 0;
}

答案 5 :(得分: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;
}

答案 6 :(得分:-1)

  

用下面的代码替换你的代码(向左移位数组)。

templ = a[0];
    for(i=0;i<n-1;i++)
    {
        a[i] = a[i+1];
        cout<<"Left: "<<a[i]<<endl;
    }
    a[n-1] = templ;
    cout<<"Left: "<<a[n-1]<<endl;