If n is 3, enter 3 numbers to find the maximum number of 3 digits:
The first scenario:
Using the conditional operator to solve the condition, the format of the conditional operator is x?y:z
X is a boolean-type expression, if x=true,
The result of the entire conditional operator is the value of Y, otherwise it is the value of Z. (((a>b) a:b)) is to get the maximum value of a/b.
int a =1,b=2,c=3;
int h= ((a>b) a:b) <c)? C: (((a>b)? a:b));
System.out.println ("The largest number is" +h);
The second scenario:
int a=1,b=2,c=3;
if (a>b&&a>c)
&& is a short-circuit with (improved logic and performance above) when the left is false and no longer takes the calculation to the right.
{
System.out.println ("The largest number is" +a);
}
else if (b>a&&b>c)
{
System.out.println ("The largest number is" +b);
}
else if (c>a&&c>b)
{
System.out.println ("The largest number is" +c);
}
The third scenario:
int a=1,b=2,c=3;
int max=a;
if (max<b)
{
Max=b;
if (max<c)
{
max=c;
}
}
else if (max<c)
{
max=c;
if (max<b)
{
max=b;
}
}
System.out.println ("The largest number is:" +max);
Fourth method, bubble sort:
Scanner s = new Scanner (system.in);
Create an array
Int[] A=new int[3];
System.out.println ("Please enter 3 numbers:");
assigning values to arrays
for (int i = 0; i < 3; i++)
{
A[i]=s.nextint ();
}
for (int i = 0; i < a.length-1; i++)
The number of bubbles is a.length-1, note not I < a.length.
{
for (int j = 0; J < A.length-1-i; J + +)//number of comparisons
Because each bubble decreases by one contrast, in order to reduce the number of computer calculations and subtract I
{
if (a[j]>a[j+1])
If a[j]>a[j+1], change position before and after, otherwise do not change position. That is, put the big numbers back.
{
int temp=a[j+1];
A[J+1]=A[J];
a[j]=temp; Set a staged value for transposition
}
}
System.out.println ("The largest number is:" +a[a.length-1]);
If n=5, the first option will be modified:
int a =1,b=2,c=3;d=4,e=5;
int h= ((a>b) a:b) <c)? C: (((a>b)? a:b));
int i= (((h>d) h:d) <e)? e: (((h>d) h:d));
System.out.println ("The largest number is" +i);
......
What if the value of n is large? has been such a comparison, obviously not smart, bubble sort and the previous scheme compared, appear more intelligent, create an array, the number to be compared to the array, and then bubble sort (write good algorithm), and finally print out the desired results. The conditioning is clear, obviously when the value of n is larger is our best choice.
Summarize:
At this point is the maximum number of 3, the first three scenarios are superior to the fourth bubble sorting scheme, the first solution code is the least amount, the fastest solution, so at this time we should choose the first solution, second and second scenario. Second, the third solution when seeking to compare the maximum value of a small number of times, the logic is relatively clearer, but when the value of n is large, it is not the best solution. For example, if you are seeking 5 numbers, or even more numbers, the code of the first several programs will be very slow, the advantages of bubble sequencing is obvious, such as whether it is from the convenience of the digital value or clear ideas, or the established algorithm, will be our better choice.
Choose the non-optimal solution, perhaps you can complete the task, but perhaps it is a lot of detours, the effort. can we also see that choice is more important than effort? The answer is yes.
This article is from the "Javase Learning and Summary" blog, please be sure to keep this source http://iamsmile.blog.51cto.com/10728276/1703987
Multiple schemes for "Maximum of n numbers", and analysis of advantages and disadvantages of various schemes.