有序序列判断 题解

有序序列判断

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

其实只需要把两种情况列出来再比对就可以了

#include<bits/stdc++.h>
using namespace std;
int n;
int a[55];
int b[55];
int c[55];
bool prepare(int x,int y){//重载sort
    return x > y;
}
int main(){
    scanf("%d",&n);
    for(int i = 1;i <= n;i ++){
        scanf("%d",&a[i]);
        b[i] = a[i];
        c[i] = a[i];
    }//读入、处理

    sort(b+1,b+n+1);
    sort(c+1,c+n+1,prepare);//排序

    bool t1 = true;
    for(int i = 1;i <= n;i ++)
        if(b[i] != a[i]){
            t1 = false;
            break;
        }
    if(t1){
        printf("sorted\n");
        return 0;
    }//判断第一种、输出

    bool t2 = true;
    for(int i = 1;i <= n;i ++)
        if(c[i] != a[i]){
            t2 = false;
            break;
        }
    if(t2){
        printf("sorted\n");
        return 0;
    }//判断第二种、输出

    printf("unsorted\n");//两次都不输出,则无序
}
全部评论

相关推荐

仁者伍敌:难怪小公司那么挑剔,让你们这些大佬把位置拿了
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务