题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include <stdio.h> #define N 55 int a[N]; int check1(int a[], int n) { for (int i = 1; i < n; i ++ ) { if (a[i] < a[i - 1]) return 0; } return 1; } int check2(int a[], int n) { for (int i = 1; i < n; i ++ ) { if (a[i] > a[i - 1]) return 0; } return 1; } int main() { int n; scanf("%d", &n); for (int i = 0; i < n; i ++ ) { scanf("%d", &a[i]); } if (check1(a, n) || check2(a, n)) printf("sorted"); else printf("unsorted"); return 0; }