2016-09-17 72 views
-4

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

请帮忙!

+0

http://www.cplusplus.com/reference/algorithm/rotate/ – deviantfan

+1

索引是错误的,它看起来像包裹在C++基本的C代码。 – xinaiz

+0

thnks的链接,但你可以请帮助我让它走了,因为它适用于正确的,我想了解我这样请 –

回答

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

非常感谢你的一切,我有一个问题:对于你移动到右边的部分,你能使它变成这样吗? –

+0

我可以让它变成正确的初始数组,不是左数组,而是最初的输出将是51234 –

+0

您需要创建第二个数组,将第一个数组向左移动,将第二个数组移到右侧(或将第一个数组向左移动两次后向右移动第一个数组)。我会编辑我的答案。 – xinaiz

1
交换元素在C++

最简单方法是使用std :: iter_swap()

所以对于4个元件阵列交换元件1和4,你会做下列

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

音符你也需要#include <algorithm>这个工作

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

-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; 
+0

如果你解释了操作代码有什么问题,你改变了什么,以及为什么,这会很有用。 –

0

或者你可以使用的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; 
} 
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; 
    } 
+0

非常感谢你 –

+0

@MelinVen欢迎你 – MichalSzczep

0

至于其他已经表示它的所有有关指标在for循环你。几乎总是有麻烦,如果你的停止条件是我< =大小,因为C++中的数组是零索引

在哪里黑摩西alogrithm是最容易理解(可能是fastes),我读你的代码为如果您尝试将数组的第一个值通过数组交换到最后一个位置,下面我试图找出这种方法。

#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; 
} 
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; 
} 
+1

plz添加一些描述为解答问题 –