• 去重 排序


    明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的随机整数(N≤100),对于其中重复的数字,只保留一个,把其余相同的数去掉,不同的数对应着不同的学生的学号。然后再把这些数从小到大排序,按照排好的顺序去找同学做调查。请你协助明明完成“去重”与“排序”的工作。     Input Param       n               输入随机数的个数       inputArray      n个随机整数组成的数组        Return Value      OutputArray    输出处理后的随机整数   注:测试用例保证输入参数的正确性,答题者无需验证。

    set集合中的数据没有顺序,且如果add两个一样的对象或基本类型的数据,set集合里也是只有一个,即set集合中的数据都是独一无二的;不能使用加强的for循环
    list中的数据是有顺序的,可以加入多个一样的对象和基本类型的数据,可使用加强的for循环

     适用javac1.7

    import java.util.*;

    public class Main{
        public static void main(String[] args){
            Scanner in = new Scanner(System.in);
            while(in.hasNextInt()){
                Set<Integer> set = new HashSet<>();
                int n = in.nextInt();
                for(int i=0;i<n;i++){
                    set.add(in.nextInt());
                }
                List<Integer> list = new ArrayList<>();
                list.addAll(set);
                Collections.sort(list);
                for(int i:list){
                    System.out.println(i);
                }
            }
        }
    }
     
     
     
    import java.util.*;
    /**
     * Created by 华夏紫云 on 2015/11/23.
     */
    public class Main {
        public static void main(String[] args) {
             Scanner sc=new Scanner(System.in);
            while (sc.hasNext()){
                int n=sc.nextInt();
                HashSet<Integer>  hashSet=new HashSet<Integer>();
                for(int i=0;i<n;i++){
                    hashSet.add(sc.nextInt());
                }
                ArrayList<Integer> arrayList=new ArrayList<Integer>();
                Iterator<Integer> iterator=hashSet.iterator();
                while (iterator.hasNext()){
                    arrayList.add(iterator.next());
     
                }
                Collections.sort(arrayList);
                //StringBuffer sb=new StringBuffer();
                for(int i=0;i<hashSet.size();i++){
                    //sb.append(arrayList.get(i) + " ");
                    System.out.println(arrayList.get(i));
                }
               // System.out.println(sb.substring(0,sb.length()-1));
            }
            }
        }
     
     
     
    先输入有几个数字
    5
    12 34 12 56 78
    输出
    12 34 56 78
       
  • 相关阅读:
    无U盘安装Linux openSUSE(通过硬盘安装Linux)
    【汇编语言】DEBUG的使用
    【汇编语言】新手第一步——HelloWorld & A+B
    Java开发中的23种设计模式详解(转)
    python安装PIL包的方法
    python正则表达式匹配十六进制数据
    mysql安装的坑
    pdfplumber解析PDF报错:ValueError: not enough values to unpack (expected 2, got 1)
    pdfplumber解析票据PDF文档,部分中文字体返回CID,无法解析
    CAN总线字节序
  • 原文地址:https://www.cnblogs.com/bb3q/p/5063539.html
Copyright © 2020-2023  润新知