题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include <stdio.h> int main() { int n = 0; int i = 0; scanf("%d ", &n); int arr[50] = { 0 }; int count1 = 0; int count2 = 0; int count3 = 0; for (i = 0; i < n; i++) { scanf("%d", &arr[i]); } for (i = 0; i < n - 1; i++) { //判断是否有序 //比了n-1次 if (arr[i] < arr[i + 1]) count1++; else if (arr[i] == arr[i + 1]) count2++; else count3++; } if ((count1 + count2 == n - 1) || (count3 + count2 == n - 1)) printf("sorted"); else printf("unsorted"); }