题解 | #有序序列插入一个整数#
有序序列插入一个整数
https://www.nowcoder.com/practice/444e87f938464906a1649cff236b102b
public class Program { public static void Main() { string result = "";
int len = int.Parse(System.Console.ReadLine()); string[] inPutNum = System.Console.ReadLine().Split(" "); int insertNum = int.Parse(System.Console.ReadLine()); bool isInsert = false; for (int i = 0; i < len; i++) { if (int.Parse(inPutNum[i]) < insertNum) { result += inPutNum[i] + " "; } else if (int.Parse(inPutNum[i]) >= insertNum) { if (!isInsert) { result += insertNum.ToString() + " "; result += int.Parse(inPutNum[i]) + " "; isInsert = true; } else { result += int.Parse(inPutNum[i]) + " "; } } } //如果走完循环还没插入说明要插入的数排最后 if (!isInsert) result += insertNum.ToString(); System.Console.Write(result); }
}