题解 | #有序序列插入一个整数#
有序序列插入一个整数
https://www.nowcoder.com/practice/444e87f938464906a1649cff236b102b
#define _CRT_SECURE_NO_WARNINGS 1 #include <stdio.h> int main() { int a; while (scanf("%d", &a) != EOF) { int arr[50] = { 0 }; for (int i = 0; i < a; i++) { scanf("%d", arr + i); } int b = 0; scanf("%d", &b); for (int i = a; i >= 0; i--) { if (b >= arr[i - 1]) { arr[i] = b; break; } else if (b > arr[i - 1] && b < arr[i]) { arr[i] = arr[i - 1]; arr[i - 1] = b; break; } else if (b < arr[i]&&i==0) { arr[i] = arr[i - 1]; arr[0] = b; } else { arr[i] = arr[i - 1]; } } for (int i = 0; i <= a; i++) { printf("%d ", arr[i]); } }//链表 return 0; }