题解 | #有序序列插入一个整数#
有序序列插入一个整数
http://www.nowcoder.com/practice/444e87f938464906a1649cff236b102b
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
boolean flag = true; // 是否需要插入
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int num = sc.nextInt();
// 输出数组
for (int i = 0; i < arr.length; i++) {
// 判断插入条件
if (arr[i] >= num && flag) {
flag = false;
System.out.print(num + " " + arr[i] + " ");
} else {
System.out.print(arr[i] + " ");
if (flag && i == arr.length - 1) {
System.out.print(num);
}
}
}
}
}