题解 | #有序序列插入一个整数#
有序序列插入一个整数
https://www.nowcoder.com/practice/444e87f938464906a1649cff236b102b
#include <stdio.h> int main() { int n, arr[52], i, val, pos; scanf("%d", &n); for (i=0; i<n; i++){ scanf("%d", &arr[i]); } scanf("%d", &val); pos = n; for (i=n-1; i>=0; i--){ if (val<=arr[i] && val>=arr[i-1]){ pos = i; //找到要插入的位置 } } for (i=n-1; i>=pos; i--){ arr[i+1] = arr[i]; } arr[pos] = val; for (i=0; i<n+1; i++){ printf("%d ", arr[i]); } return 0; }