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

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

My code looks like this so far:

public class ThreeSort {     public static void main(String[] args) {          int num1 = Integer.parseInt(args[0]);         int num2 = Integer.parseInt(args[1]);         int num3 = Integer.parseInt(args[2]);           int x = Math.min(num1, num2);         int min = Math.min(x,num3);         int z = Math.max(num1, num2);         int max = Math.max(z, num3);          int a = 0;         int mid = 0;            while (mid >= min && mid <= max) {          mid = a;          }          System.out.println(min);         System.out.println(a);         System.out.println(max);        } 

I know how to do the min and the max but I'm having troubles with the middle one. Any idea how to do that without conditional statements?

回答1:

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; 


回答2:

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); 


回答3:

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.



回答4:

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]; 


回答5:

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)) 


回答6:

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);      } } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!