#include<iostream>
#include<string>
#include<vector>
#include<stack>
#include<stdlib.h>
#include"algorithm"
using namespace std;
void reOrderArray(vector<int> &array)
{
//双向队列
deque<int> result;
int num = array.size();
for(int i = 0; i < num; i++)
{
//int array[] = {1, 2, 8, 9, 3, 6};
//这样做的结果是 1 9 3 2 8 6
if(array[num-i-1] % 2 ==1)
{
result.push_front(array[num-i-1]);
}
if(array[i] % 2 == 0)
{
result.push_back(array[i]);
}
/* 这样做的结果是 3 9 1 2 8 6 破坏了数组原来的相对顺序
if(array[i] % 2 == 1)
{
result.push_front(array[i]);
}
else if(array[i] % 2 == 0)
{
result.push_back(array[i]);
}*/
}
array.assign(result.begin(), result.end());
}
int main()
{
int array[] = {1, 2, 8, 9, 3, 6};
vector<int> v1;
v1.assign(array, array + 6);
reOrderArray(v1);
for(vector<int>::iterator it = v1.begin(); it != v1.end();it++)
{
cout << *it << endl;
}
return 0;
}
10、调整数组顺序使奇数位于偶数前面
最新推荐文章于 2023-04-11 18:14:39 发布