题解 | #明明的随机数#

明明的随机数

https://www.nowcoder.com/practice/3245215fffb84b7b81285493eae92ff0

import java.util.Scanner;
import java.util.ArrayList;
import java.util.Comparator;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        int rows = 0;
        int num = 0;
        ArrayList<Integer> arrayList = new ArrayList<Integer>();
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            if (num == 0) {
                rows = in.nextInt();
            } else {
                int  nuber = in.nextInt();
                if (arrayList.size() >= rows) {
                    break;
                }
                if (!arrayList.contains(nuber)) {
                    arrayList.add(nuber);
                }

            }
            num++;
        }
        arrayList.sort(Comparator.naturalOrder());

        for (int i = 0; i < arrayList.size(); i++) {
            System.out.println(arrayList.get(i));
        }
    }
}

全部评论

相关推荐

嵌入式的小白:简历关键的就是项目经历,你这密密麻麻的,我一点开就不想看了,每一条都不换行,而且每一个里面写那么多,需要精简一下,这样别人看一眼就能知道你做了啥,用了啥技术
点赞 评论 收藏
分享
求个付费实习岗位:这种就是吃满时代红利又没啥技术水平,只能靠压力学生彰显优越感的老登,别太在意了
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务