码迷,mamicode.com
首页 > 其他好文 > 详细

奇前偶后

时间:2018-11-21 17:45:14      阅读:253      评论:0      收藏:0      [点我收藏+]

标签:ace   for   一个   turn   序列   ios   clu   bsp   cstring   

描述

给定10个整数的序列,要求对其重新排序。排序要求:

1.奇数在前,偶数在后;

2.奇数按从大到小排序;

3.偶数按从小到大排序。

输入输入一行,包含10个整数,彼此以一个空格分开,每个整数的范围是大于等于0,小于等于100。输出按照要求排序后输出一行,包含排序后的10个整数,数与数之间以一个空格分开。样例输入

4 7 3 13 11 12 0 47 34 98

样例输出

47 13 11 7 3 0 4 12 34 98
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
 
int main()
{
   int a[100],b[100];
    int n,m;
    int t=0,k=0;
    for(int i=0;i<10;i++)
     {
	 cin>>m;
	  if(m%2!=0)
	   {
	      a[t++]=m;    //注意这种写法
	    }
	   else
	   {
	     b[k++]=m; 
	   }
     }
    sort(a,a+t);
    sort(b,b+k);
     
    for(int i=t-1;i>=0;i--)
     {
	  cout<<a[i]<<" ";
     }
   for(int i=0;i<k;i++)
    {
 	   cout<<b[i]<<" ";
    }   
    return 0;
}

  

 

奇前偶后

标签:ace   for   一个   turn   序列   ios   clu   bsp   cstring   

原文地址:https://www.cnblogs.com/fangzheng-nie/p/9996003.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!