题解 | #有序序列判断#

有序序列判断

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

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] a = new int[n];
        for (int i = 0; i < n; i++) {
            int x = in.nextInt();
            a[i] = x;
        }
        int q = 0;
        for (int i = 0; i < n; i++) {
            if (i < n - 1) {
                if (a[1] < a[0]) {
                    for(int j=i+1;j<n;j++){
                        if(a[i]<a[j]){
                            break;
                        }else{
                            q += 1;
                        }
                    }

                } else {
                    for (int j = i + 1; j < n; j++) {
                        if (a[i] > a[j]) {
                            break;
                        } else {
                            q += 1;
                        }
                    }
                }
            }
        }
        if (q == (n * (n - 1)) / 2) {
            System.out.println("sorted");
        } else {
            System.out.println("unsorted");
        }
    }
}

全部评论

相关推荐

03-26 13:44
南华大学 Java
在看面经的花生米很野蛮:这种情况下你当然要回答,你也是吗!!!!我超喜欢他的XXXXX
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务