Most efficient way to find the greatest of three ints

前端 未结 14 1119
离开以前
离开以前 2020-12-30 07:58

Below is my pseudo code.

function highest(i, j, k)
{
  if(i > j && i > k)
  {
    return i;
  }
  else         


        
14条回答
  •  醉梦人生
    2020-12-30 08:37

    I think by "most efficient" you are talking about performance, trying not to waste computing resources. But you could be referring to writing fewer lines of code or maybe about the readability of your source code. I am providing an example below, and you can evaluate if you find something useful or if you prefer another version from the answers you received.

    /* Java version, whose syntax is very similar to C++. Call this program "LargestOfThreeNumbers.java" */
    class LargestOfThreeNumbers{
        public static void main(String args[]){
            int x, y, z, largest;
            x = 1;
            y = 2;
            z = 3;
            largest = x;
            if(y > x){
                largest = y;
                if(z > y){
                    largest = z;
                }
            }else if(z > x){
                largest = z;
            }
            System.out.println("The largest number is: " + largest);
        }
    }
    

提交回复
热议问题