【发布时间】: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 的建议。
-
哇。那就是......那里的一段代码。很好地分解这样的代码,但是用你的蜘蛛侠感觉来处理这样的事情。通常我会开始计划这样的事情,想:“必须有更好的方法来做到这一点。”然后我用谷歌搜索。