键盘输入一串数字(0-9)   按照下列要求输出  1.数字最多的排前面 2.数字相同时大的排前面
比如  1 2 1 3 4    输出   1 4 3 2

解决方案 »

  1.   

    实现的比较弱智:
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Set;
    import java.util.TreeSet;/**
     * 键盘输入一串数字(0-9) 按照下列要求输出 1.数字最多的排前面 2.数字相同时大的排前面
     * 比如 1 2 1 3 4 输出 1 4 3 2
     *
     */
    public class SortTest
    {
    public static void main(String[] args) throws IOException
    {
    BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("请输入一串数字,以空格分隔:");
    String input  = buf.readLine();

    if(null == input)
    return;
    String[] array = input.split("\\s+");

    int length;
    if(0 == (length = array.length))
    return;

    Set<NumTime> set = new TreeSet<NumTime>();

    for(int i = 0; i < length; i ++)
    {
    int num = Integer.parseInt(array[i]);
    NumTime nt = new NumTime();
    nt.num = num;

    NumTime nn = getSame(nt,set);
    if(null == nn)
    nt.times = 1;
    else
    nt.times = ++ nn.times;

    set.add(nt);
    }

    System.out.println("排序后:");
    for(NumTime n : set)
    {
    System.out.println(n.num + "\t出现次数: " + n.times);
    }
    }
    private static NumTime getSame(NumTime n, Set<NumTime> set)
    {
    if(null == n)
    return null;
    if(null == set)
    return n;

    for(NumTime ntt : set)
    {
    if(ntt.equals(n))
    {
    //顺便删除它
    set.remove(ntt);
    return ntt;
    }
    }
    return null;
    }
    private static class NumTime implements Comparable<NumTime>
    {
    int num;
    int times;

    @Override
    public boolean equals(Object obj)
    {
    NumTime other = (NumTime)obj;
    return other.num == this.num;
    }
    @Override
    public int hashCode()
    {
    return num;
    }
    @Override
    public int compareTo(NumTime o)
    {
    int ot = o.times;
    int on = o.num;
    return ot > times ? 1 : (ot == times ? (on > num ? 1 : (on == num ? 0 : -1)) : -1);
    }
    }
    }
      

  2.   

    使用map存储当前数字的权值。遍历一次即可。
    权值计算方法如下:
    如果map中不含有该数字,设置该数字权值为该数字的值,
    否则将权值+10(因为数的范围是0-9)
    最后根据权值大小输出。比如1,2,1,3,4,1,2,3
    得到的权值为:
    1 21
    2 12
    3 13
    4 4
    所以结果为1,3,2,4
      

  3.   

    定义一个长度为10的整型数组,下标为0到9.遍历整个输入的串,遇到数组就给对应的下标项自增1.然后将这个长度为10的整型数组排序。排序函数比较的时候自己另外写一个Comparator类当成第二个参数,先比较数组中对应项的值然后比较对应的下标。至于排序算法,冒泡就很合适
      

  4.   

    package com.zzm;import java.util.Collection;
    import java.util.HashMap;
    import java.util.Scanner;/**
     * 键盘输入一串数字(0-9) 按照下列要求输出 1.数字最多的排前面 2.数字相同时大的排前面
     * 0-9 是关键
     * @author Water Zhang
     *
     */
    public class Demo1 { /**
     * @param args
     */
    public static void main(String[] args) {
    Scanner in = new Scanner(System.in);  
    //<数值,权值>
    HashMap<Integer,Integer> map = new HashMap<Integer,Integer>();
    System.out.println("输入非数值字符退出...");
    while(in.hasNextInt()){
    int one = in.nextInt();
    if(drop(one))
    continue;
     if(map.containsKey(one)){
     int weight = map.get(one);
     map.put(one, weight+10);
     }else{
     map.put(one, one);
     }
    }

    Collection<Integer> result =  map.values();
    // System.out.println(result);
    //依次输出最大值
    while(!result.isEmpty()){
    int max =0;
    for(Integer one : result){
    if(one> max)
    max = one;
    }
    result.remove(max);
    System.out.print(max%10 + " ");
    }
    } /**
     * 是否丢掉
     * @param num
     * @return
     */
    public static boolean drop(int num){
    if(num>9 || num<0){
    System.out.println("Drop: "+ num);
    return true;
    }
    return false;
    }
    }
    按照4楼的想法,这是我的实现