Algorithm Note _159: algorithm to increase the second largest integer (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

  1 problem description Problem DescriptionWrite 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 sampleSample Input5 8-12 7 0Sample Output7

2 Solutions

The specific code is as follows:

Importjava.util.ArrayList;Importjava.util.Collections;ImportJava.util.Scanner; Public classMain { Public Static voidMain (string[] args) {ArrayList<Integer> list =NewArraylist<integer>(); Scanner in=NewScanner (system.in);  while(true) {            intA =In.nextint (); if(A = = 0)                 Break;        List.add (a);        } collections.sort (list); System.out.println (List.get (list.size ()-2)); }}

Algorithm Note _159: algorithm to increase the second largest integer (Java)

Related Article

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.