c語言實現奇偶排序演算法_C 語言

來源:互聯網
上載者:User

=====
第2題:奇偶排序(一)
=====


總時間限制:
1000ms
記憶體限制:
65536kB
描述
輸入十個整數,將十個整數按升序排列輸出,並且奇數在前,偶數在後。
輸入
輸入十個整數
輸出
按照奇偶排序好的十個整數

複製代碼 代碼如下:

#include<stdio.h>

#define  COUNT 10
#define bool int
#define true 1
#define false 0


/****
*負責冒泡排序
***/
int* sortFunction(int data[]){
 int i,j;
 for(j=0;j<COUNT-1;j++){
  for(i=0;i<COUNT-1-j;i++){

    if (data[i]>data[i+1]){
     int tmp=data[i]; data[i]=data[i+1];data[i+1]=tmp;
    }

  }

 }
 return data;
}

/****
**負責判斷奇數或者偶數
*/
bool isOddNumber(int data){

 if(data % 2==0){
  return false;
 }else{
  return true;
 }
}

main()
{
 int arr[]={10, 9 ,8, 7 ,6, 5, 4, 3, 2, 1};
 int oddArr[COUNT]={0},evenArr[COUNT]={0};
 int *tmp=NULL;
 int i;
 int m=0;
 int n=0;
 for(i=0;i<COUNT;i++){
  if(isOddNumber(arr[i])){
   evenArr[m++]=arr[i];
  }else{
   oddArr[n++]=arr[i];
  }
 }

 //對於奇數數組排序
 tmp=sortFunction(oddArr);
 puts("\n========odd number==========\n");
 for(i=0;i<COUNT;i++){
  int num=*tmp;
  if(num!=0)
  {
   printf("%d ",num);
  }
  *tmp++;
 }

 

 //對於偶數數組排序
 tmp=sortFunction(evenArr);
 puts("\n========even number==========\n");
 for(i=0;i<COUNT;i++){
  int num=*tmp;
  if(num!=0)
  {
   printf("%d ",num);
  }
  *tmp++;
 }

 printf("over");
}


以上代碼編寫與vs2012裡完成。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.