将数组C[1:n]中所有奇数移到偶数之前,要求时间复杂度为O(n)
答案:4 悬赏:50 手机版
解决时间 2021-04-05 18:06
- 提问者网友:活着好累
- 2021-04-05 14:49
将数组C[1:n]中所有奇数移到偶数之前,要求时间复杂度为O(n)
最佳答案
- 五星知识达人网友:煞尾
- 2021-04-05 15:59
快速排序的思想!
int *rev(int *a, int n)
{
int *p, *q;
int temp;
p = a;
q = a + n - 1;
while(p < q)
{
while((*p)%2) p++;
while(!((*q)%2)) q--;
if(p < q)
{
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
}
return a;
}
int *rev(int *a, int n)
{
int *p, *q;
int temp;
p = a;
q = a + n - 1;
while(p < q)
{
while((*p)%2) p++;
while(!((*q)%2)) q--;
if(p < q)
{
temp = *p;
*p = *q;
*q = temp;
p++;
q--;
}
}
return a;
}
全部回答
- 1楼网友:雾月
- 2021-04-05 18:47
#include
#include
#include
inline bool isodd(int i){return i%2 != 0;}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int b[sizeof a/sizeof a[0]];
std::partition(a,a+sizeof a/sizeof a[0],isodd);
std::copy(a,a+sizeof a/sizeof a[0],b);
std::copy(b,b+sizeof b/sizeof b[0],std::ostream_iterator(std::cout," "));
system("pause");
}
#include
#include
inline bool isodd(int i){return i%2 != 0;}
int main()
{
int a[]={1,2,3,4,5,6,7,8,9};
int b[sizeof a/sizeof a[0]];
std::partition(a,a+sizeof a/sizeof a[0],isodd);
std::copy(a,a+sizeof a/sizeof a[0],b);
std::copy(b,b+sizeof b/sizeof b[0],std::ostream_iterator
system("pause");
}
- 2楼网友:愁杀梦里人
- 2021-04-05 18:10
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void shift(int a[], int n)
{
int first_not_odd = -1;
int i;
for ( i=0; i {
if ( a[i] % 2 )
swap( &a[i], &a[ ++ first_not_odd ]);
}
}
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void shift(int a[], int n)
{
int first_not_odd = -1;
int i;
for ( i=0; i
if ( a[i] % 2 )
swap( &a[i], &a[ ++ first_not_odd ]);
}
}
- 3楼网友:往事隔山水
- 2021-04-05 17:35
void swap(int *a, int *b)
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void shift(int a[], int n)
{
int first_not_odd = -1;
int i;
for ( i=0; i {
if ( a[i] % 2 )
swap( &a[i], &a[ ++ first_not_odd ]);
}
}
int main()
{
int a[10];
shift(a, 10);
}
{
int tmp = *a;
*a = *b;
*b = tmp;
}
void shift(int a[], int n)
{
int first_not_odd = -1;
int i;
for ( i=0; i
if ( a[i] % 2 )
swap( &a[i], &a[ ++ first_not_odd ]);
}
}
int main()
{
int a[10];
shift(a, 10);
}
参考资料:我刚才没登陆~~呵呵
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯