C语言题目13

IT小小龙 posted @ 2014年4月06日 16:23 in C with tags 排序 奇数 偶数 , 666 阅读
//有两列数字, A{1,2,3,4,5,6,7,8,9}   B{10,11,12,13,14,15} 
//合并成一个新的数列C,要求所有的奇数排在所有的偶数前面,
//并且奇数要升序,偶数要降序,输出数列C到屏幕上

#include"stdio.h"
int getminodd(int c[], int start, int n)
{
  int k;
  int minodd = start;
  while(c[minodd]%2 == 0)
    {
      minodd = minodd+1;
      if(minodd == n)
	{
	  break;
	}
    }
  if(minodd != n)
    {
      for(k=minodd; k<n; k++)
	{
	  if((c[minodd]>c[k]) && c[k]%2 == 1)
	    {
	      minodd = k;
	    }
	}
    } 
  return minodd;
}

int getmaxeven(int c[], int start, int n)
{
  int k;
  int maxeven = start;
  while(c[maxeven]%2 != 0)
    {
      maxeven = maxeven+1;
      if(maxeven == n)
	{
	  break;
	}
    }
  if(maxeven != n)
    {
      for(k=maxeven; k<n; k++)
	{
	  if((c[maxeven]<c[k]) && c[k]%2 == 0)
	    {
	      maxeven = k;
	    }
	}
    } 
  return maxeven;
}

int main()
{
  int a[9] = {1,2,3,4,5,6,7,8,9};
  int b[6] = {10,11,12,13,14,15};
  int c[15] = {};
  int i;
  int j;
  int k=0;
  int count=0;
  int minodd=0;
  int maxeven=0;
  int swap;
  //合并
  for(i=0; i<9; i++)
    {
      c[k] = a[i];
      k = k+1;
    }
  for(j=0; j<6; j++)
    {
      c[k] = b[j];
      k = k+1;
    }
  //奇数部分选择排序
  while(1)
    {
      minodd = getminodd(c, count, 15);
      if(minodd == 15)
	{
	  break;
	}
      swap = c[count];
      c[count] = c[minodd];
      c[minodd] = swap;
      count = count+1;
      if(count >= 15)
	{
	  break;
	}
    }
  //偶数部分选择排序
  while(1)
    {
      maxeven = getmaxeven(c, count, 15);
      if(maxeven == 15)
	{
	  break;
	}
      swap = c[count];
      c[count] = c[maxeven];
      c[maxeven] = swap;
      count = count+1;
      if(count >= 15)
	{
	  break;
	}
    }
  //输出
  for(k=0; k<15; k++)
    {
      printf("%d ", c[k]); 
    }
  printf("\n");
  return 1;
}
Avatar_small
PAN India 说:
2022年8月09日 14:56

PAN is abbreviated as Presence Across Nation and is operating or available at every possible location. A mark of PAN India is given to an organization or firm or company if their entity or branches are spread across every state and their customers can avail of their services from anywhere in India. PAN India If any company operates at one location at the start and has spread its branches to various cities of India, then that company can use the recognition to announce its company growth.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter