3 numbers in ascending order WITHOUT the use of conditional statements in Java. As in I can't use if statements at all

前端 未结 6 1033
旧巷少年郎
旧巷少年郎 2021-01-25 00:24

My code looks like this so far:

public class ThreeSort {
    public static void main(String[] args) {

        int num1 = Integer.parseInt(args[0]);
        int          


        
相关标签:
6条回答
  • 2021-01-25 00:56

    Put them in a List and sort it...

    List<Integer> ints = new LinkedList<>();
    ints.add(Integer.parseInt(args[0]));
    ints.add(Integer.parseInt(args[1]));
    ints.add(Integer.parseInt(args[2]));
    
    Collections.sort(ints); // smallest -> greatest
    System.out.println(ints);
    
    Collections.reverse(ints); // greatest -> smallest
    System.out.println(ints);
    
    0 讨论(0)
  • 2021-01-25 01:05

    in this case there is a simple algorithm for it:

    mid = Math.max(Math.min(num1,num2), Math.min(Math.max(num1,num2),num3));
    

    Also as the operator ^ denotes bitwise xor. so another way is:

    mid=num1^num2^num3^max^min;

    EXAMPLE:

    public static void main(String[] args) {
        System.out.println(mid(70, 3, 10));
    }
    
    static int mid(int a, int b, int c) {
        int mx = Math.max(Math.max(a, b), c);
        int mn = Math.min(Math.min(a, b), c);
        int md = a ^ b ^ c ^ mx ^ mn;
        return md;
    }
    

    OUTPUT: 10.

    Also as OldCurmudgeon said below you can calculate the mid with below formula:

    int mid = num1 + num2 + num3 - min - max;
    
    0 讨论(0)
  • 2021-01-25 01:11
    int mid = num1 + num2 + num3 - min - max;
    

    Sorry for briefness - posted from my phone.

    It must be self-evident that the middle number is the sum of the three numbers minus the max minus the min. Would also work if max == mid or max == min or even both.

    0 讨论(0)
  • 2021-01-25 01:16

    Assuming this is some kind of puzzle/homework are you allowed to use the ternary operator?

    int[] ints = {3, 1, 2};
    int min = ints[0] <= ints[1] && ints[0] <= ints[2]
              ? ints[0]
              : ints[1] <= ints[0] && ints[1] <= ints[2]
                ? ints[1]
                : ints[2];
    
    0 讨论(0)
  • 2021-01-25 01:16

    This is how I would implement three_sort without any if statements or ternary operators. You would have to adapt this to your language of choice.

    def two_sort(a, b):
        small = int(a < b)  * a + int(a >= b) * b
        large = int(a >= b) * a + int(a < b)  * b
        return small, large
    
    def three_sort(a, b, c):
        a, b = two_sort(a, b)
        a, c = two_sort(a, c)
        b, c = two_sort(b, c)
        return a, b, c
    

    for a more general solution:

    from random    import randint
    
    def two_sort(a, b):
        small = int(a < b)  * a + int(a >= b) * b
        large = int(a >= b) * a + int(a < b)  * b
        return small, large
    
        return li[-1]
    
    def n_sort(li):
        for _ in li:
            for i, _ in enumerate(li[:-1]):
                li[i], li[i+1] = two_sort(li[i], li[i+1])
        return li
    
    N = 10
    li = [randint(0, 1000) for _ in range(N)]
    print(N_Sort(N)(*li))
    
    0 讨论(0)
  • 2021-01-25 01:17
    public class ThreeSort {
        public static void main(String[] args) {
    
            // command-line input
            int a = Integer.parseInt(args[0]);
            int b= Integer.parseInt(args[1]);
            int c = Integer.parseInt(args[2]);
    
    
            // compute the order
    
            int max=Math.max(Math.max(a,b),c);
            int min =Math.min(Math.min(a,b ), c);
            int middle = a + b + c - max - min;
    
    
            // output in ascending order
    
            System.out.println(min+"\n" + middle+ "\n"+ max);
    
        }
    }
    
    0 讨论(0)
提交回复
热议问题