pta c++ 6-2 奇偶分离排序 (10分)

输入10个整数,完成一个函数使数据 重新排序以后输出(也按空格分隔),要求: 输出奇数在前偶数在后

函数接口定义:

void sort_tarray ( int* a );
 

裁判测试程序样例:

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
void sort_array(int* a);
/* 请在这里填写答案 */
int main(){
    int a[10];
    for (int i=0;i < 10; i++)
		cin>>a[i]; 
   sort_array(a);
    return 0;
}

 

输入样例:

在这里给出一组输入。例如:

1 2 3 4 5 6 7 8 9 10
 

输出样例:

在这里给出相应的输出。例如:

9 7 5 3 1 2 4 6 8 10
 
作者
王秀
单位
福州大学
代码长度限制
16 KB
时间限制
400 ms
内存限制
64 MB

 

int cmp(int a,int b){
    if(
    (a%2==1&&b%2==0)
    ||((a%2==1&&b%2==1)&&a>b)
    ||((a%2==0&&b%2==0)&&a<b)
    )return 1;
 return 0;
} 
void sort_array ( int* a ){
    sort(a,a+10,cmp);
    for (int i=0;i< 10; i++ ){
        cout<<a[i];
        if(i!=9)cout<<" ";
    }
} 

 

 

 

posted on 2020-08-14 16:28  xwwer  阅读(619)  评论(0)    收藏  举报