题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include <stdio.h> int main() { int n = 0, flag = 0; scanf("%d", &n); int arr[50] = { 0 }; for (int i = 0;i < n;i++) scanf("%d", &arr[i]); for (int i = 0;i < n;i++) { for (int j = i + 1;j < n;j++) if (arr[i] > arr[j]) { flag++; } } if (flag == 0 || flag == n * (n - 1) / 2) printf("sorted"); else printf("unsorted"); return 0; } //比较抽象,通过逐个两个相比统计左大于右的次数