版权声明:欢迎转载,也请注明原文地址 https://blog.csdn.net/wzy_2017/article/details/80011570
数列有序!
Time Limit: 1000 ms
Memory Limit: 65536 KiB
Problem Description
有n(n<=100)个整数,已经按照从小到大顺序排列好,现在另外给一个整数m,请将该数插入到序列中,并使新的序列仍然有序。
Input
输入数据包含多个测试实例,每组数据由两行组成,第一行是n和m,第二行是已经有序的n个数的数列。n和m同时为0表示输入数据的结束,本行不做处理。
Output
对于每个测试实例,输出插入新的元素后的数列。
Sample Input
3 3 1 2 4 0 0
Sample Output
1 2 3 4
Hint
Source
HDOJ
package leslie1;
//import java.io.*;
//import java.math.*;
//import java.text.*;
//import java.math.BigInteger;
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
while (cin.hasNextLine()) {
int n = cin.nextInt();
int m = cin.nextInt();
if (n == 0 && m == 0)
break;
else {
List<Integer> l = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
l.add(cin.nextInt());
l.add(m);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
System.out.printf("%d%c", l.get(i), i == l.size() - 1 ? '\n' : ' ');
}
}
cin.close();
}
}