有序序列判断 题解
有序序列判断
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");//两次都不输出,则无序
}
查看13道真题和解析
字节跳动公司福利 1309人发布