标题有序序列判断
#include<stdio.h>
int main()
{
int n = 0;
int arr[50] = { 0 };//存放原序列
int arr1[50] = { 0 };//存放升序列
int arr2[50] = { 0 };//存放降序列
scanf("%d", &n);//获取n
for (int i = 0; i < n; i++)//将序列放入arr,arr1,arr2中
{
scanf("%d", &arr[i]);
arr1[i] = arr[i];
arr2[i] = arr[i];
}
//升序
for (int i = 0; i < n - 1; i++)
{
if (arr1[i] > arr1[i + 1])
{
int tmp = arr1[i];
arr1[i] = arr1[i + 1];
arr1[i + 1] = tmp;
}
}
//降序
for (int i = 0; i < n - 1; i++)
{
if (arr2[i] < arr2[i + 1])
{
int tmp = arr2[i];
arr2[i] = arr2[i + 1];
arr2[i + 1] = tmp;
}
}
//判断升序和降序之后的序列和原序列是否一致,一致则有序
int i = 0;
for (i = 0; i < n; i++)
{
if (arr[i] != arr1[i] && arr[i] != arr2[i])//arr,arr1,arr2对应元素都不同,
//则肯定无序
{
printf("unsorted\n");//打印
break; //并终止循环
}
}
if (i == n)//如果在上面循环中找不到对应元素都不同的,则原序列有序
{
printf("sorted\n");
}
return 0;
}