解题思路:    链表存储,每次记录最大的数字,循环查找并替换

注意事项:

参考代码:

import java.util.LinkedList;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		String s = in.next();
		int n = in.nextInt();
		in.close();		
		int a[] = new int[s.length()];		
		for(int i=0;i<s.length();i++){
			char temp =s.charAt(i);
			a[i] = temp -'0';//不减去‘0’则会获得Ascii码	
		}	
		LinkedList<Integer> linkedList = new LinkedList<Integer>();
		for(int i=0;i<a.length;i++){
			linkedList.add(a[i]);
		}		
		int flag =0;
		while(flag<n){
			for(int i=0;i<linkedList.size()-1;i++){
				if(linkedList.get(i)<linkedList.get(i+1)){
					linkedList.remove(i);//使用链表移出元素
					flag++;				
					break;//结束本次循环,跳转到while循环中
				}
				//考虑到特殊情况,当遍历完全部数字都不满足条件,从末尾删除数字
				if(i==linkedList.size()-2){
					linkedList.removeLast();
					flag++;
				
				}
			}
		}
		for(int i=0;i<linkedList.size();i++){			
			System.out.print(linkedList.get(i));
		}
	}
}


 

0.0分

1 人评分

  评论区