The Blue Bridge Cup Algorithm Improves Reverse Sorting

Original title link: http://lx.lanqiao.cn/problem.page?gpid=T215

Welcome to my Blue Bridge Cup OJ Problem Solution~ https://blog.csdn.net/richenyunqi/article/details/80192062

 Algorithm to improve reverse order  
Time limit: 1.0s Memory limit: 512.0MB
Problem Description
  Write a program that reads in a set of integers (up to 20) and stores them in an array of integers. When the user enters 0, it indicates the end of the input. The program will then re-store the values ​​in this array in reverse order and print them. 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 according to Re-store them in reverse order, that is, they become 2 6 -5 19 7, and then print them out.
  Input format: The input is only one line, consisting of several integers, separated by spaces, and the last integer is 0.
  Output format: The output also has only one line, that is, the integers arranged in reverse order, separated by spaces, and there is no space at the end.
  Input and output example
sample input
7 19 -5 6 2 0
Sample output
2 6 -5 19 7
java code:
import java.util.Scanner;  
public class Main{  
    public static void main(String[] args){  
        Scanner input=new Scanner(System.in);  
        int[] num=new int[20];  
        int i;  
        for(i=0;;i++){  
            int a=input.nextInt();  
            if(a==0)  
                break;  
            num[i]=a;  
        }  
        for(i=i-1;i>=0;i--){  
            System.out.print(num[i]);  
            if(i!=0)  
                System.out.print(" ");  
        }
        System.out.println();  
    }  
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325738233&siteId=291194637