zoukankan      html  css  js  c++  java
  • 九度OJ:题目1117:整数奇偶排序

     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             else
    18                 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("
    ");
    36     }
    37     return 0;
    38 }

    由于数据可能是有机有偶,只有奇数,只有偶数,要分不同情况讨论,空格的处理非常重要

  • 相关阅读:
    vim 使用总结
    C语言中的条件编译
    getopt函数
    Linux 命令使用总结
    vim ctags cscope
    shell 定时处理
    shell 脚本中的标准输出
    ubuntu mail 命令发送邮件
    shell 编程中的特殊字符的处理
    system v 消息队列
  • 原文地址:https://www.cnblogs.com/Evence/p/4448480.html
Copyright © 2011-2022 走看看