题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include <stdio.h> int main() { int arr[50] = {0}; int n = 0; while (1 == scanf("%d", &n)) { // 记录升序 int greater = 0; int less = 0; int equal = 0; // 输入数据 for (int i = 0; i < n; i++) { scanf("%d", &arr[i]); } // 判断是否有序 for (int i = 0; i < n - 1; i++) { if (arr[i] < arr[i + 1]) { greater++; } else if (arr[i] == arr[i + 1]) { equal++; } else if (arr[i] > arr[i + 1]) { less++; } } if ((n - 1 == less) || (greater == n - 1) || (equal == n - 1)) { printf("sorted\n"); } else { printf("unsorted\n"); } } return 0; }