题解 | #有序序列判断#
有序序列判断
https://www.nowcoder.com/practice/22e87f8a8d764a6582710f38d1b40c6e
#include<stdio.h>
int main() {
int n,i,j,flag=0,title=0,sum=0;
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++) {
scanf("%d ", &a[i]);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(a[i]<=a[j]){
flag+=1;
}
else
title+=1;
}
}
while(n>=1){
sum+=n-1;
n--;
}
if(flag==sum){
printf("sorted");
}
else if (title==sum){
printf("sorted");
}
else
printf("unsorted");
}
int main() {
int n,i,j,flag=0,title=0,sum=0;
scanf("%d", &n);
int a[n];
for (i = 0; i < n; i++) {
scanf("%d ", &a[i]);
}
for(i=0;i<n-1;i++){
for(j=i+1;j<n;j++){
if(a[i]<=a[j]){
flag+=1;
}
else
title+=1;
}
}
while(n>=1){
sum+=n-1;
n--;
}
if(flag==sum){
printf("sorted");
}
else if (title==sum){
printf("sorted");
}
else
printf("unsorted");
}