题解 | #有序序列判断#

有序序列判断

http://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e

int main() {
    int n, max = 0, min = 0, equal = 0;
    scanf("%d", &n);
    int m[n];
    for (int i = 0; i < n; i++)
        scanf("%d ", &m[i]);
    for (int j = 1; j < n; j++) {  //判断三种有序序列的状态
        if (m[j] - m[j - 1] < 0) max++;
        if (m[j] - m[j - 1] > 0) min++;
        if (m[j] - m[j - 1] == 0) equal++;
    }
    //判断三种有序序列的状态是否正确
    if (min == n - 1 || max == n - 1 || equal == n - 1)
        printf("sorted");
    else
        printf("unsorted");
    return 0;
}
全部评论

相关推荐

1 收藏 评论
分享
牛客网
牛客企业服务