您的位置:首页 > 其它

整数数组,奇数在前,偶数在后

2012-12-01 19:19 357 查看
// 奇数在前,偶数在后,
#include <iostream>
#include <assert.h>
#include <algorithm>
using namespace std;

void swapIntArr(int *an, int n)
{
assert(NULL !=an);
int *p1=an, *p2=an+n-1;
while (p1 < p2)
{
while (*p1 %2 !=0)
{
p1++;
}

while (*p2 %2 == 0)
{
p2--;
}
int t=*p1;
*p1=*p2;
*p2=t;
p1++;
p2--;
}
}
int main()
{

int an[]={3,5,2,5,2,6,8,3,25,65,67,78,34,2,32,3};
int n=sizeof(an)/sizeof(int);
copy(an,an+n,ostream_iterator<int>(cout, " "));
cout<<endl;

swapIntArr(an,n);
copy(an,an+n,ostream_iterator<int>(cout, " "));
cout<<endl;
return 0;
}


整数数组,奇数在前,偶数在后。

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐