Second largest integer

Source: Internet
Author: User

Problem description

Write a program that reads in a set of integers (no more than 20), and when the user enters 0 o'clock, the input ends. The program will then find the second largest integer from this set of integers and print it out. Note: (1) 0 indicates the end of the input, which itself is not counted in this set of integers. (2) In this set of integers, both positive and negative numbers are possible. (3) The number of integers in this group is not less than 2.
Input format: Enter only one line, including several integers, separated by a space, the last integer is 0.
Output format: Output the second largest integer.
Input/Output sample

Sample input

5 8-12 7 0

Sample output

7

/***************************************************************************************************

Import java.util.*;

public class Main

{

PUBILC void Show (int arr[],int i)

{

int j,count=0;

First, we're going to create a new array to hold the nonzero number.

Next we want to create a count to locate our input number in the position of the array (similar to the role of the pointer)

int arrs[]=new Int[i];

for (j=0;j<arr.length;j++)

{

if (arr[j]!=0)

{

ARRS[COUNT]=ARR[J];

count++;

}

}

System.out.println (Arrs[arrs.length-2]);

}

public static void Main (String args[])

{

Preparing for traversing an array I

int i,n,arr[]=new int[20];

Scannner sc=new Scanner (system.in);

Define a variable to hold the number N of each input in the array, and to end the traversal of the array when we enter 0

for (i=0;i<arr.length;i++)

{

Arr[i]=sc.nextint ();

N=arr[i];

if (n!=0) arr[i]=sc.nextint ();

}else break;

Arrays.sort () through Java;

Arrays.sort (arr);

Order, now think about, if it is to conquer the number of mixed or positive, so that we directly output ARR[ARR.LENGTH-2]; this will do.

But if all are negative, because we initialize the allocation space, the other places are not assigned to the value of 0, so we have to deal with;

So let's write a show () to show our handling

Show (Arr,i);

        

}

}

*************************************************************************************************************** *******/

The above is a personal write, the following way through the collection, we can get through the collection of List.add () and get (), thus reducing the space above the code array, resulting in extra space, and occupy 0, the result of our results have the effect of the problem

Import java.util.*;

public class Main

{

Defines a collection array, with generic

Scanner sc=new Scanner (system.in);

List<integer> list=new arrarylist<> ();

int a=0;

while ((A=sc.nextint ())!=0);

{

List.add (a);

}

Collections.sort (list);

System.out.println (Llist.get (List.size ()-2));

}

Second largest integer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.