数组中,将奇数都靠左,偶数都靠右;并且将左边的奇数升序,右边的偶数降序

源程序:

#include <iostream>
using namespace std;

const int n=10;

typedef int array[n];
void main()
{
  int start,end;
  array a={60,34,55,78,90,99,76,85,96,43};
  int m[10];
  int c[6];
  array &b=a;
  a[2]=101;
  //奇数靠左,偶数靠右

  start=0;
  end=n-1;

  for(int i=0;i<n;i++)
  {
    if(b[i]%2==1)
      m[start++]=a[i];
    else
      m[end--]=b[i];
  }
  //奇数由小到大排序,偶数由大到小排序
  for(i=0;i<start;i++)
  {
    for(int j=0;j<start-i-1;j++)
    {
      if(m[j]>=m[j+1])
      {
        int temp=m[j];
        m[j]=m[j+1];
        m[j+1]=temp;
      }
    }
  }

  for(i=start;i<=10;i++)
  {
    for(int j=i-1;j<start+10-i-1;j++)
    {
      if(m[j]<=m[j+1])
      {
        int temp=m[j];
        m[j]=m[j+1];
        m[j+1]=temp;
      }
    }
  }
  // printf("start=%d ",start);
  // printf("end=%d ",n-start);
  for(i=0;i<n;i++)
    cout<<m[i]<<" ";

}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/11802988.html