1 #include "stdafx.h" 2 #include <iostream> 3 /* 4 题目:调整数组顺序使奇数位于偶数前面 5 输入一个整数数组,实现一个函数来调整该数组中数字的顺序,使得所有奇数位于数组的前半部分,所有偶数位于数组的后半部分. 6 */ 7 using namespace std; 8 9 void ReorderOddEvent(int *pData,unsigned int length) 10 { 11 int left = 0,right = length-1; 12 while(left<right) 13 { 14 while(pData[left]%2==1) 15 { 16 left++; 17 } 18 while(pData[right]%2==0) 19 { 20 right--; 21 } 22 if(left<right) 23 { 24 int temp = pData[left]; 25 pData[left]=pData[right]; 26 pData[right] = temp; 27 } 28 } 29 cout<<length<<endl; 30 } 31 int _tmain(int argc, _TCHAR* argv[]) 32 { 33 int nums[]={1,2,3,4,5,6,7,8,3,4,6}; 34 int length = sizeof(nums)/sizeof(int); 35 ReorderOddEvent(nums,length); 36 for(int i = 0;i!=length;++i) 37 cout<<nums[i]<<" "; 38 return 0 ; 39 }
原文:http://www.cnblogs.com/crazycodehzp/p/3561591.html