java奇偶排序算法_整数奇偶排序

17

//充分发挥sort的能力嘛,cmp中就可以把所有逻辑写清楚了

#include

#include

using namespace std;

bool cmp(int x,int y)

{

if(x&1 && y&1)    //如果是奇数就大到小

return x>y;

if(x%2==0 && y%2==0)    //好像用位运算判断偶数不好使??

return x

if(x&1)        //奇数在前

return true;

else

return false;

}

int main()

{

int array[10];

while(scanf("%d",&array[0])!=EOF)

{

for(int i = 1; i < 10; i++)

scanf("%d",&array[i]);

sort(array,array+10,cmp); //sort很强的啊

for(int i = 0; i<9; i++)

printf("%d ",array[i]);

printf("%d\n",array[9]);

}//while

return 0;

}//main

发表于 2018-02-27 22:47:53

回复(0)

8

python两行代码搞定,就是这么b while True:

try:

a=list(map(int,input().split()))

print(" ".join(map(str,sorted(filter(lambda c:c%2==1,a),reverse=True)+sorted(filter(lambda c:c%2==0,a)))))

except:

break

发表于 2017-10-06 15:35:46

回复(2)

3

#include//三步走1.从小到大排序2.从后到前输出奇数3.从前向后输出偶数

#define n 10

int main()

{

int a[n],i,j,t;

for(i=0;i

scanf("%d",&a[i]);

for(i=0;i

for(j=0;j

if(a[j]>a[j+1])//从小到大

{t=a[j];a[j]=a[j+1];a[j+1]=t;}//交换

for(i=n-1;i>=0;i--) //2.从大到小输出奇数

if(a[i]%2!=0)//奇数

printf("%d ",a[i]);

for(i=0;i

if(a[i]%2==0)//偶数

printf("%d ",a[i]);

}

编辑于 2020-04-02 18:23:33

回复(0)

3

发现输出的数组具有很好的对称性,哦~

先用sort排好序,再借用p、q这两个小指针,一个从前往后,一个从后往前重新放数在另一个数组中

#include 

#include 

#include 

using namespace std;

int main(){

int a[10] = {0};

int b[10] = {0};

while (cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9]) {

int p = 0, q = 9; //两个小“指针”

sort(a, a + 10);

for (int i = 9; i > -1; i--) { //从后往前扫描输入的数组

if (a[i]%2 != 0){ //如果是奇数

b[p] = a[i]; //从前往后放

p++;

} else { //如果是偶数

b[q] = a[i]; //从后往前放

q--;

}

}

for (int j = 0; j 

printf("%d ", b[j]);

}

printf("\n");

}

return 0;

}

编辑于 2020-02-16 21:05:33

回复(2)

2

分类sort两次 #include

#include

using namespace std;

int ji[10], ou[10];

int ji_len, ou_len;

bool cmp_s2b(int a, int b){

return a < b;

}

bool cmp_b2s(int a, int b){

return a > b;

}

int main(){

int a[10];

while(cin >> a[0] >> a[1] >> a[2] >> a[3] >> a[4] >> a[5] >> a[6] >> a[7] >> a[8] >> a[9]){

ji_len = 0;

ou_len = 0;

for(int i = 0; i < 10; i++){

if(a[i] % 2)

ji[ji_len++] = a[i];

else

ou[ou_len++] = a[i];

}

sort(ji, ji + ji_len, cmp_b2s);

sort(ou, ou + ou_len, cmp_s2b);

for(int i = 0; i < ji_len; i++)

cout << ji[i] << ' ';

for(int i = 0; i < ou_len; i++){

cout << ou[i];

if(i != ou_len - 1)

cout << ' ';

}

}

return 0;

}

发表于 2019-03-14 20:11:08

回复(0)

1

压根不用思考,直接sort然后后向打印再前向打印即可。 #include

using namespace std;

int main()

{

int a[10]={0};

while(cin>>a[0]>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]>>a[7]>>a[8]>>a[9])

{

sort(a,a+10);

for(int i=9;i>=0;--i)

if(a[i]%2==1)

cout<

for(int i=0;i<=9;++i)

if(a[i]%2==0)

cout<

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值