简体   繁体   English

如何输入n个数字并以升序打印而不使用数组

[英]how to input n numbers and print it in ascending order without using array

I was trying to create a program that takes n number of input in a loop and capable of showing output in ascending order. 我试图创建一个程序,该程序在一个循环中接收n个输入,并且能够以升序显示输出。 The main theme of program is it should not use arrays. 程序的主要主题是它不应使用数组。

I tried it by creating a program that takes input and shows smallest number first then 2nd smallest and 3rd smallest and so on. 我通过创建一个接受输入并首先显示最小数字,然后显示第二最小和第三最小等等的程序来进行尝试。 But I am new to programming so my program could do only like 1st smallest and it does not work. 但是我是编程的新手,所以我的程序只能像最小的第一个那样工作,并且不起作用。

I also saw heap data structure in one of the similar questions but couldn't figure out how it can work without arrays. 我在类似的问题之一中也看到了堆数据结构,但无法弄清没有数组如何工作。

Please help me how can we use heap data structure or give me a good suggestion to do it. 请帮助我如何使用堆数据结构,或者给我一个很好的建议。

List<Integer> list = new LinkedList<Integer>();
Scanner scanner = new Scanner(System.in);

while (true) {
    System.out.print("Enter a number, -1 to exit: ");
    int num = scanner.nextInt();

    if (num == -1) break;

    list.add(num);
}

// sort the list (ascending)
Collections.sort(list);

// output the list
for (Integer val : list) {
    System.out.println(val);
}

You could use Streams: 您可以使用流:

    StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(new Scanner(System.in), Spliterator.ORDERED),
            false)
            .limit(10)
            .sorted()
            .forEach(System.out::println);

Use SortedSet for default sorting, without duplicates. 使用SortedSet进行默认排序,不重复。 (Don't if duplicates are needed here!) (如果这里不需要重复!)

    SortedSet<Integer> sortedSet = new TreeSet<Integer>();
    Scanner scanner = new Scanner(System.in);

    while (true) {
        System.out.print("Enter a number, -1 to exit: ");
        int num = scanner.nextInt();
        if (num == -1) 
            break;
        sortedSet.add(num);
    }
    System.out.println(sortedSet);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在不使用集合的情况下按升序打印偶数和按降序打印奇数 - How to print even numbers in ascending order and odd numbers in descending order without using collection 以递归方式按升序打印所有数字组合(长度 n 在 1 到 9 之间) - Print all cominations of numbers in ascending order recursively (with length n between 1 and 9) 在不使用数组或列表类型结构的情况下读入N流的输入流并以相反的顺序打印? - Read in N Lines of an Input Stream and print in reverse order without using array or list type structure? 按升序打印二进制数字 - print binary numbers in ascending order 如何使用Scanner在Main中输入两个数字并使其与方法一起使用(升序Java)? - How to input two numbers in Main using Scanner and make it work with a method (Ascending order java)? 如何使用java按升序生成数字的顺序? - How to generate a sequential order of numbers in ascending order using java? 如何使用一种方法以升序和降序输入数组? - How do I use a method to input an array in ascending and descending order? 对于具有整数升序的N个大小相等的数组,如何选择数组常用的数字? - For N equally sized arrays with integers in ascending order, how can I select the numbers common to arrays? 如何将此数组按升序排序? - How to sort this array into ascending order? 如何在Java中的arraylist中按升序排列数字? - How to put numbers in ascending order in an arraylist in Java?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM