九度OJ:題目1117:整數奇偶排序

來源:互聯網
上載者:User

標籤:

 1 #include<stdio.h> 2 #include<algorithm> 3 using namespace std; 4 const int N=10; 5 int main() 6 { 7     int arr[10],odd[10],even[10]; 8     while(scanf("%d",&arr[0])!=EOF) 9         {10         for(int i=1 ; i<N ; ++i)11             scanf("%d",&arr[i]);12         sort(arr,arr+N);13         int io=0,ie=0;14         for(int i=0 ; i<N ; ++i)15             if(arr[i]%2==0)16                 even[ie++]=arr[i];17             else18                 odd[io++]=arr[i];19         bool mark=0;                    //標記有沒有奇數20         if(io!=0)21             mark=1;22         while(io!=0)                    //從後向前列印奇數23         {24             printf("%d",odd[--io]);25             if(io!=0)26                 printf(" ");27         }28         int cnt=0;29         while(cnt!=ie)                    //從前向後列印偶數30         {31             if(0==cnt && 0==mark)        //【caution】之前沒有奇數,開頭不可為空格32                 printf("%d",even[cnt++]);33             printf(" %d",even[cnt++]);34         }35         printf("\n");36     }37     return 0;38 }

由於資料可能是有機有偶,只有奇數,只有偶數,要分不同情況討論,空格的處理非常重要

九度OJ:題目1117:整數奇偶排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.