zoukankan      html  css  js  c++  java
  • 去重 排序

    明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了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
       
  • 相关阅读:
    git线上操作
    IDEA快捷方式
    Java 四种线程池
    java 获取当前天之后或之前7天日期
    如何理解AWS 网络,如何创建一个多层安全网络架构
    申请 Let's Encrypt 通配符 HTTPS 证书
    GCE 部署 ELK 7.1可视化分析 nginx
    使用 bash 脚本把 AWS EC2 数据备份到 S3
    使用 bash 脚本把 GCE 的数据备份到 GCS
    nginx 配置 https 并强制跳转(lnmp一键安装包)
  • 原文地址:https://www.cnblogs.com/bb3q/p/5063539.html
Copyright © 2011-2022 走看看