题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include <stdio.h> int bubble1(int* p, int n) { int i, j; int t; for (i = 1; i <= n - 1; i++) { for (j = 1; j <= n - i; j++) { if (p[j] > p[j + 1]) { t = p[j]; p[j] = p[j + 1]; p[j + 1] = t; return 1; //1表示修改过 } } } return 0; } int bubble2(int* p, int n) { int i, j; int t; for (i = 1; i <= n - 1; i++) { for (j = 1; j <= n - i; j++) { if (p[j] < p[j + 1]) { t = p[j]; p[j] = p[j + 1]; p[j + 1] = t; return 1; //1表示修改过 } } } return 0; } int main() { int n, arr[100], i, x, y, brr[100]; scanf("%d", &n); for (i = 1; i <= n; i++) scanf("%d", &arr[i]); for (i = 1; i <= n; i++) brr[i] = arr[i]; x = bubble1(arr, n); y = bubble2(brr, n); if (x == 0 || y == 0) printf("sorted\n"); else if (x == 0 && y == 0) printf("sorted\n"); else printf("unsorted\n"); return 0; } 想了很久就只想出这样做了,可能比较麻烦,记录一下😂