keep the largest number

Topic description

Given a decimal positive integer number, choose to remove a part of the numbers from it, and hope that the remaining numbers will form the largest positive integer.

Enter description:

The input is two lines, the first line is a positive integer number, 1 ≤ length(number) ≤ 50000. The second line is the number of digits to be removed cnt 1 ≤ cnt < length(number).

Output description:

Output the retained results.
Example 1

enter

325 1

output

35
1  import java.util.ArrayList;
 2  import java.util.Arrays;
 3  import java.util.Collection;
 4  import java.util.List;
 5  import java.util.Scanner;
 6  /** 
7  * 
 8  * keep max Number,
 9  * The number of digits is fixed, let the number on the left be as large as possible 
 10  * Use i to loop to ensure that there is no number larger than i in front of i, move i backward,
 11   */ 
12  public  class Main {
 13      public  static  void main(String[] args) {
 14          // input 
15          Scanner sc =new Scanner(System.in);
 16          String num = sc.nextLine();
 17          int n = sc.nextInt();
 18          // Process 
19          String[] ss = num.split("" );
 20          List<String > list = new ArrayList();
 21          list.addAll(Arrays.asList(ss));
 22            //    1, 
23          for ( int i = 1; i < ss.length; i++) { // loop to the end of list and exit 
24              if (n == 0 ) {
 25                  break ; //   remove n bits and exit
26              }
 27              while (i>0&&i<list.size() // Guaranteed not to exceed the bounds 
28                      &&list.get(i).compareTo(list.get(i - 1)) > 0) { // The loop ensures that there is no size before i His big number is 
29                  if (n == 0 ) {
 30                      break ;   // remove n bits and exit 
31                  }
 32                  list.remove(i - 1 );
 33                  i--; // remove the subscript pointed to by the preceding i To subtract one 
34                  n-- ;
 35              }
 36          }
 37          //   2,
38          while (n > 0) { // If the list has been sorted from large to small, and the number of digits is not enough n, then take 
39 from the tail              list.remove(list.size() - 1 );
 40              n-- ;
 41          }
 42          String res = "" ;
 43          for (String s : list) {
 44              res += s;
 45          }
 46          System.out.println(res);
 47      }
 48 }

 

Guess you like

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