Java:不使用数组排序元素

时间:2017-10-26 08:34:56

标签: java arrays eclipse

我有一个家庭作业,我应该提示用户输入五个数字并从最小到最大排列,因为我们没有采取数组

我只剩下Math.min& Math.max或if语句。

我写了一个代码,其中第一个,第二个和最后一个数字总是正确但我似乎无法弄清楚如何做第三和第四个数字

以下是一个例子:

    if (a <= b && a <= c && a <= d && a <= e) {
        System.out.println("Numbers in Ascending order "
                + a + " " +Math.min(Math.min(b, c), Math.min(d, e)) +
                " " + "?" +
                " " + "?" +
                " " +Math.max(Math.max(b, c), Math.max(d, e)));
    }

如果您知道任何可能有助于我解决此任务的想法?

4 个答案:

答案 0 :(得分:2)

这是一种可能的解决方案

public static void main(String... args) {
    int a = 32;
    int b = 42;
    int c = 2;
    int d = 88;
    int e = 92901;
    int counter = 0;
    while (counter < 5) {
        int currentMin = findMin(a, b, c, d, e);

        // Printing smallest number yeat
        System.out.print(currentMin + " ");

        if (a == currentMin){
            a = Integer.MAX_VALUE;
        }

        if (b == currentMin){
            b = Integer.MAX_VALUE;
        }

        if (c == currentMin){
            c = Integer.MAX_VALUE;
        }

        if (d == currentMin){
            d = Integer.MAX_VALUE;
        }

        if (e == currentMin){
            e = Integer.MAX_VALUE;
        }
        counter++;
    }
}

private static int findMin(int a, int b, int c, int d, int e) {
    int smallest = Math.min(a, Math.min(b, Math.min(c, Math.min(d, e))));
    return smallest;
}

注意我如何使用Integer.MAX_VALUE删除最小数字,例如在第一次迭代2中将返回等于c现在我必须使代码以某种方式在下一次迭代中忽略c,因为它如果我使用Integer对象已经被使用了我可以将c设置为null但是int不能被设置为null所以我能做的就是将它设置为数字这么大以至于findMin函数永远不会再选择它这就是我使用的原因MAX_VALUE

答案 1 :(得分:2)

如果你必须使用 if else if 语句,那么你必须写一个 if 语句和119 否则,如果语句,即可以安排5个数字的方式。 5!= 120。

如果您被允许使用循环检查this链接

答案 2 :(得分:0)

int aux;
if (b < a){
    aux = b; b = a; a = aux;
}
if (c < a){
    aux = c; c = b; b = a; a = aux;
}
else{
    if (c < b){
        aux = c; c = b; b = aux;
    }
}
...

我想你明白了,最终a将是最小的e将是最大的

有关详细信息,因为看起来您已经开始编程和排序算法,这称为插入排序(我相信)。更多信息,请https://en.wikipedia.org/wiki/Insertion_sort

答案 3 :(得分:0)

这是一个可能的答案。由于无法使用循环和数组,因此大部分代码都是重复的。最后,a,b,c,d,e包含从min到max排列的值。希望这会有所帮助。

import java.io.*;

class SortFiveElements {

    //utility function
    //returns index as a:1, b:2, c:3, d:4, e:4
    public static int find_min_index(int a,int b, int c,int d, int e, int min)
    {
        return a==min?1:(b==min?2:(c==min)?3:(d==min?4:5));
    }

    public static void main (String[] args) throws IOException{
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int a = Integer.parseInt(br.readLine());
        int b = Integer.parseInt(br.readLine());
        int c = Integer.parseInt(br.readLine());
        int d = Integer.parseInt(br.readLine());
        int e = Integer.parseInt(br.readLine());

        //temp is a temporary var to store the i-th value which may get replaced
        //smallest stores the min value among i-th to 5th element
        //idx stores the minimum value's index
        int temp,smallest,idx;

        //i=1,  i.e element 'a'
        //temp has value of 1st element that is a
        //find minimum among 5 elements in 'smallest', its index in 'idx'
        //then swap
        temp = a;
        smallest = Math.min(a,Math.min(b,Math.min(c,Math.min(d,e))));
        idx = find_min_index(a,b,c,d,e,smallest);
        a = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        //i=2,  i.e element 'b'
        //temp has value of 2nd element that is b
        //find minimum among 4 elements in 'smallest', its index in 'idx'
        //NB: a already has the smallest value, so replace a with MAX_VALUE while finding index
        //then swap
        temp = b;
        smallest = Math.min(b,Math.min(c,Math.min(d,e)));
        idx = find_min_index(Integer.MAX_VALUE,b,c,d,e,smallest);
        b = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        //repeat above process for 'c' and 'd'.
        //'e' will automatically fall in place
        temp = c;
        smallest = Math.min(c,Math.min(d,e));
        idx = find_min_index(Integer.MAX_VALUE,Integer.MAX_VALUE,c,d,e,smallest);
        c = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        temp = d;
        smallest = Math.min(d,e);
        idx = find_min_index(Integer.MAX_VALUE,Integer.MAX_VALUE,
                             Integer.MAX_VALUE,d,e,smallest);
        d = smallest;
        if(idx==1)
            a=temp;
        else if(idx==2)
            b = temp;
        else if(idx==3)
            c = temp;
        else if(idx==4)
            d = temp;
        else
            e = temp;

        //we have the values in sorted order in a,b,c,d,e
        System.out.println(a+" "+b+" "+c+" "+d+" "+e);
    }
}