• 奇偶数入座


    奇偶数入座
    描述: 将输入的一串10个整数进行从小到大排序,但是要求偶数放在偶数位置上,奇数放在奇数位置上。缺失的位置由0补齐,输入中不会有0。

    运行时间限制: 无限制
    内存限制: 128 MByte
    输入: 输入10组随机整数

    输出: 按照规则排序后输出

    样例输入: 53 8 21 43 3 7 51 62 13 68

    样例输出: 3 8 7 62 13 68 21 0 43 0 51 0 53

    答案提示:

     1 import java.io.BufferedReader;
     2 import java.io.IOException;
     3 import java.io.InputStreamReader;
     4 import java.util.ArrayList;
     5 
     6 public class Sort {
     7     //排序
     8     public static void sort(ArrayList<Integer> arr){
     9         for (int i = 0; i < arr.size(); i++) {
    10             for (int j = 0; j < i; j++) {
    11                 if (arr.get(j) > arr.get(j+1)) {
    12                     int tmp = arr.get(j);
    13                     arr.set(j, arr.get(j+1));
    14                     arr.set(j+1, tmp);
    15                 }
    16             }
    17         }
    18     }
    19 
    20     public static  ArrayList<Integer> getSort(String str) {
    21 
    22         ArrayList<Integer> even = new ArrayList<Integer>();
    23         ArrayList<Integer> odd = new ArrayList<Integer>();
    24         ArrayList<Integer> total = new ArrayList<Integer>();
    25         String[] in = str.split(" ");
    26         
    27         for (int i = 0; i < in.length; i++) {
    28             // 划分奇数和偶数
    29             if (Integer.valueOf(in[i]) % 2 == 0) {
    30                 even.add(Integer.valueOf(in[i]));
    31             } else {
    32                 odd.add(Integer.valueOf(in[i]));
    33             }
    34         }        
    35 
    36         //分别对奇数和偶数排序
    37         Sort.sort(odd);
    38         Sort.sort(even);
    39 
    40         // 不够的用0填充,奇数少
    41         if (odd.size() >= even.size()) {
    42             int size = odd.size() - even.size();
    43             //System.out.println("差值:"+size);
    44             for (int j = 0; j < size; j++) {
    45                 even.add(0);
    46             }
    47         } else {
    48             //如果偶数少
    49             int size = even.size() - odd.size();
    50             for (int j = 0; j < size ; j++) {
    51                 odd.add(0);
    52             }
    53         }
    54         //将后来的全部输存入数组
    55         int m = odd.size();
    56         System.out.println("m="+m);
    57         for (int j = 0; j < m; j++) {
    58             total.add(odd.get(j));
    59             total.add(even.get(j));
    60         }
    61         //去除最后一个0
    62         if (total.get(total.size()-1)==0) {
    63             total.remove(total.size()-1);
    64         }
    65         System.out.println("最后排序结果:");
    66         for (int i = 0; i < total.size(); i++) {
    67             System.out.print(total.get(i) + " ");
    68         }
    69         return total;
    70     }
    71 
    72     public static void main(String[] args) {
    73 
    74         //String input = "53 8 21 43 3 7 51 62 13 68";
    75         BufferedReader buf = new BufferedReader(
    76                 new InputStreamReader(System.in));
    77         try {
    78             String str = buf.readLine();
    79             getSort(str);
    80         } catch (IOException e) {
    81             e.printStackTrace();
    82         }
    83     
    84     }
    85 }
  • 相关阅读:
    数据库概念相关
    JavaWeb基础知识总结
    MyBatis学习总结(二)——使用MyBatis对表执行CRUD操作
    MyBatis学习总结(一)——MyBatis快速入门
    [源码解析]HashMap和HashTable的区别(源码分析解读)
    MyBatis学习总结(三)——优化MyBatis配置文件中的配置
    MyBatis学习总结(四)——解决字段名与实体类属性名不相同的冲突
    MyBatis学习总结(五)——实现关联表查询
    MyBatis学习总结(六)——调用存储过程
    MyBatis学习总结(七)——Mybatis缓存
  • 原文地址:https://www.cnblogs.com/purple-blog/p/3366615.html
Copyright © 2020-2023  润新知