Algorithm Note _158: Algorithm improves reverse order (Java)

Source: Internet
Author: User

Directory

1 Problem Description

2 Solutions

1 problem description Problem DescriptionWrite a program that reads a set of integers (no more than 20) and stores them in an integer array. When the user enters 0 o'clock, the input ends. The program then stores the values in the array in reverse order and prints them out. For example: Suppose the user enters a set of data: 7 19-5 6 2 0, then the program will save the first five valid data in an array, that is, 7 19-5 6 2, and then put the values in this array back in reverse order, which becomes 2 6-5 19 7, and then print them out.
input Format: Enter only one row, consisting of several integers, separated by a space, the end of the integer is 0.
output format: The output is only one line, that is, the integer after the reverse order, separated by a space, there is no space at the end.
input/Output sampleSample Input7 19-5 6 2 0Sample Output2 6-5 7

2 Solutions

The specific code is as follows:

Importjava.util.ArrayList;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); }         for(inti = List.size ()-1;i >= 0;i--) System.out.print (List.get (i)+" "); }}

Algorithm Note _158: Algorithm improves reverse order (Java)

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.