L1-027 for rent (20 points)
The following is a picture of the fire which once was very popular.
For a time, there was a cry for help on the internet, asking how to break it. In fact, this code is very simple, the index array is the subscript of the ARR array, index[0]=2 corresponds to arr[2]=1, index[1]=0 corresponds to arr[0]=8, index[2]=3 corresponds to arr[3]=0, and so on. It's easy to get a phone number of 18013820100.
This topic requires you to write a program to generate this code for any phone number - in fact, as long as you generate the first two lines, the latter content is unchanged.
Input format:
Input in a row gives a mobile phone number composed of 11 digits.
Output format:
Generate the first two lines of code for the input number, where the number in arr must be given in descending order.
Input sample:
18013820100
Output sample:
int[] arr = new int[]{8,3,2,1,0};
int[] index = new int[]{3,0,4,3,1,0,2,4,3,4,4};
import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); ArrayList<Integer> alist = new ArrayList<Integer>(); String s = sc.nextLine();//Get the phone number //Store phone numbers in alist arrays for (int i = 0; i < s.length(); i++) { if (!alist.contains(Integer.valueOf(s.charAt(i) + ""))) { alist.add(Integer.valueOf(s.charAt(i) + "")); } } //Sort (from small to large) Collections.sort(alist); //Reverse order Collections.reverse(alist); System.out.print("int[] arr = new int[]{"); //output for (int i = 0; i < alist.size(); i++) { if (i == 0) { System.out.print(alist.get(i)); } else { System.out.print("," + alist.get(i)); } } System.out.println("};"); System.out.print("int[] index = new int[]{"); //Subscripts for order of output telephone numbers for (int i = 0; i < s.length(); i++) { if (i == 0) { System.out.print(alist.indexOf(Integer.valueOf(s.charAt(i) + ""))); } else { System.out.print("," + alist.indexOf(Integer.valueOf(s.charAt(i) + ""))); } } System.out.println("};"); } }