描述
已知顺序表L递增有序,编写一个算法,将X插入到线性表的适当位置上,以保持线性表的有序性!
输入
12 23 34 45 56
30
输出
12 23 30 34 45 56
参考代码
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; public class Main { public static void main(String[] args) throws IOException { BufferedReader cin = new BufferedReader(new InputStreamReader(System.in)); String sn[] = cin.readLine().split(" "); List<Integer> list = new ArrayList<Integer>(); for(int i = 0;i < sn.length;i ++){ list.add(Integer.parseInt(sn[i])); } int n = Integer.parseInt(cin.readLine()); list.add(n); Collections.sort(list); Iterator<Integer> it = list.iterator(); while(it.hasNext()){ System.out.print(it.next()+" "); } System.out.println(); } }