【问题标题】:Is there a faster way to arrange numbers according to value?有没有更快的方法来根据值排列数字?
【发布时间】:2014-01-07 04:33:36
【问题描述】:

这里是新手。我已经想出了如何将数字从高到低排列,但这似乎是一种非常低效的方法,尤其是对于较大的数字组。我试图让五个数字(由用户输入)从高到低排列,整个事情从那里开始膨胀;下面是该程序的不完整代码。仅当第一个数字也具有最高值时才有效。

请告诉我有更快的方法来做到这一点。

import java.io.*;
import javax.swing.JOptionPane;
class Integers
{
public static void main(String [] args) throws Exception
{


String str1 = JOptionPane.showInputDialog("Please enter the first number.");
String str2 = JOptionPane.showInputDialog("Please enter the second number.");
String str3 = JOptionPane.showInputDialog("Please enter the third number.");
String str4 = JOptionPane.showInputDialog("Please enter the fourth number.");
String str5 = JOptionPane.showInputDialog("Please enter the fifth number.");
int a = Integer.parseInt(str1);
int b = Integer.parseInt(str2);
int c = Integer.parseInt(str3);
int d = Integer.parseInt(str4);
int e = Integer.parseInt(str5);

// 1 abcde

if ((a>b) && (b>c) && (c>d) && (d>e))
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(d);
System.out.println(e);
}

// 2 abced

if ((a>b) && (b>c) && (c>e) && (e>d))
{
System.out.println(a);
System.out.println(b);
System.out.println(c);
System.out.println(e);
System.out.println(d);
}

// 3 abdce

if ((a>b) && (b>d) && (d>c) && (c>e))
{
System.out.println(a);
System.out.println(b);
System.out.println(d);
System.out.println(c);
System.out.println(e);
}

// 4 abdec

if ((a>b) && (b>d) && (d>e) && (e>c))
{
System.out.println(a);
System.out.println(b);
System.out.println(d);
System.out.println(e);
System.out.println(c);
}

// 5 abecd

if ((a>b) && (b>e) && (e>c) && (c>d))
{
System.out.println(a);
System.out.println(b);
System.out.println(e);
System.out.println(c);
System.out.println(d);
}

// 6 abecd

if ((a>b) && (b>e) && (e>c) && (c>d))
{
System.out.println(a);
System.out.println(b);
System.out.println(e);
System.out.println(c);
System.out.println(d);
}

// 7 acbde

if ((a>c) && (c>b) && (b>d) && (d>e))
{
System.out.println(a);
System.out.println(c);
System.out.println(b);
System.out.println(d);
System.out.println(e);
}

// 8 acbed

if ((a>c) && (c>b) && (b>e) && (e>d))
{
System.out.println(a);
System.out.println(c);
System.out.println(b);
System.out.println(e);
System.out.println(d);
}

// 9 acdbe

if ((a>c) && (c>d) && (d>b) && (b>e))
{
System.out.println(a);
System.out.println(c);
System.out.println(d);
System.out.println(b);
System.out.println(e);
}

// 10 acdeb

if ((a>c) && (c>d) && (d>e) && (e>b))
{
System.out.println(a);
System.out.println(c);
System.out.println(d);
System.out.println(e);
System.out.println(b);
}

// 11 acebd

if ((a>c) && (c>e) && (e>b) && (b>d))
{
System.out.println(a);
System.out.println(c);
System.out.println(e);
System.out.println(b);
System.out.println(d);
}

// 12 acedb

if ((a>c) && (c>e) && (e>d) && (d>b))
{
System.out.println(a);
System.out.println(c);
System.out.println(e);
System.out.println(d);
System.out.println(b);
}

// 13 adbce

if ((a>d) && (d>b) && (b>c) && (c>e))
{
System.out.println(a);
System.out.println(d);
System.out.println(b);
System.out.println(c);
System.out.println(e);
}

// 14 adbec

if ((a>d) && (d>b) && (b>e) && (e>c))
{
System.out.println(a);
System.out.println(d);
System.out.println(b);
System.out.println(e);
System.out.println(c);
}

// 15 adcbe

if ((a>d) && (d>c) && (c>b) && (b>e))
{
System.out.println(a);
System.out.println(d);
System.out.println(c);
System.out.println(b);
System.out.println(e);
}

// 16 adceb

if ((a>d) && (d>c) && (c>e) && (e>b))
{
System.out.println(a);
System.out.println(d);
System.out.println(c);
System.out.println(e);
System.out.println(b);
}


// 17 adebc

if ((a>d) && (d>e) && (e>b) && (b>c))
{
System.out.println(a);
System.out.println(d);
System.out.println(e);
System.out.println(b);
System.out.println(c);
}

// 18 adecb

if ((a>d) && (d>e) && (e>c) && (c>b))
{
System.out.println(a);
System.out.println(d);
System.out.println(e);
System.out.println(c);
System.out.println(b);
}

// 19 adecb

if ((a>d) && (d>e) && (e>c) && (c>b))
{
System.out.println(a);
System.out.println(d);
System.out.println(e);
System.out.println(c);
System.out.println(b);
}

// 20 aebdc

if ((a>e) && (e>b) && (b>d) && (d>c))
{
System.out.println(a);
System.out.println(e);
System.out.println(b);
System.out.println(d);
System.out.println(c);
}

// 21 aecbd

if ((a>e) && (e>c) && (c>b) && (b>d))
{
System.out.println(a);
System.out.println(e);
System.out.println(c);
System.out.println(b);
System.out.println(d);
}

// 22 aecdb

if ((a>e) && (e>c) && (c>d) && (d>b))
{
System.out.println(a);
System.out.println(e);
System.out.println(c);
System.out.println(d);
System.out.println(b);
}

// 23 aedbc

if ((a>e) && (e>d) && (d>b) && (b>c))
{
System.out.println(a);
System.out.println(e);
System.out.println(d);
System.out.println(b);
System.out.println(c);
}

// 24 aedcb

if ((a>e) && (e>d) && (d>c) && (c>b))
{
System.out.println(a);
System.out.println(e);
System.out.println(d);
System.out.println(c);
System.out.println(b);
}


}
}

【问题讨论】:

  • 在打印数据之前,可以先排序!
  • 您应该阅读有关排序算法的信息,请参阅此处以比较各种算法:en.wikipedia.org/wiki/…
  • 查看下面的答案应该是一个好的开始,但肯定有比您所做的更快的方法。然而,我相信你做了你能做的。所以请继续练习,祝你好运,欢迎来到 SO。
  • 你的方法在执行中并不是那么低效,尽管它的代码效率非常低。我不会费心尝试将其扩展到 6 个数字。对于真正低效的算法,请查看Bogosort 及其变体。不过,要将时间花在有价值的事情上,请遵循 Manish 的建议。
  • 哇。那就是......那里的一段代码。很好地分解这样的代码,但是用你的蜘蛛侠感觉来处理这样的事情。通常我会开始计划这样的事情,想:“必须有更好的方法来做到这一点。”然后我用谷歌搜索。

标签: java loops math


【解决方案1】:

你应该使用一个数组:

int[] numbers = new int[5];

然后遍历数组并在每个索引处赋值。如果您打算对数组进行排序,请调用Arrays.sort(numbers);

【讨论】:

  • 是的,使用数组并在通过循环 Google 排序运行它时比较值。
  • 可能只是填满数组然后调用Arrays.sort()?
  • OP 希望输出按降序排列。
  • 可能这也可以工作(未测试):Arrays.sort(array, Collections.reverseOrder())
  • @Manish 只有当数组是 Integer[] 而不是 int[] 时才有效
【解决方案2】:

一种方法是:

import java.util.Arrays;
import java.util.Collections;    
import javax.swing.JOptionPane;

... // Class declaration

public static void main(String[] args) throws Exception
{
    String str1 = JOptionPane.showInputDialog("Please enter the first number.");
    String str2 = JOptionPane.showInputDialog("Please enter the second number.");
    String str3 = JOptionPane.showInputDialog("Please enter the third number.");
    String str4 = JOptionPane.showInputDialog("Please enter the fourth number.");
    String str5 = JOptionPane.showInputDialog("Please enter the fifth number.");

    Integer[] arr = new Integer[5]; // Use an array
    arr[0] = Integer.parseInt(str1);
    arr[1] = Integer.parseInt(str2);
    arr[2] = Integer.parseInt(str3);
    arr[3] = Integer.parseInt(str4);
    arr[4] = Integer.parseInt(str5);
    Arrays.sort(arr, Collections.reverseOrder());

    for (int i : arr)
        System.out.println(i);
}

注意: 数组arrInteger 类型(在Java 中,int 类型是原始类型,而Integer 类型是对象。您可能需要搜索更多相关信息)使用Arrays.sort 方法处于下降模式。

【讨论】:

    【解决方案3】:

    使用ArrayList 存储int 值并对ArrayList 进行排序。

    如果你使用Array

    int[] arr=new int[5];
    Arrays.sort(arr);
    

    如果你使用List

    List<Integer> list=new ArrayList<>();
    Collections.sort(list);
    

    然后您将获得按升序排列的值。要按降序获取此值,您可以按相反顺序获取值。

    【讨论】:

    • OP 希望输出按降序排列。
    • @TedHopp 是的,在对数组或列表进行升序排序后,OP 可以以相反的顺序获取值。
    【解决方案4】:

    这可能会有所帮助:)

    public static void main(String[] args) throws Exception
    {
        List<Integer> list=new ArrayList<Integer>();
        String NumStr="";
        do{
            NumStr = JOptionPane.showInputDialog("Please enter a number.");
            try {
                list.add(Integer.parseInt(NumStr));
            } catch (NumberFormatException numberFormatException) {
                NumStr="";
            }
        }while(!"".equals(NumStr));
    
        Collections.sort(list);
    
        for (int i : list)
            System.out.println(i);
    }
    

    当输入空字符串或字母时程序终止

    【讨论】:

      猜你喜欢
      • 2022-12-01
      • 2023-04-07
      • 1970-01-01
      • 2011-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-14
      • 2019-10-17
      • 1970-01-01
      相关资源
      最近更新 更多