翻转数组:
个人基本思路:
从头遍历数组,找到逆置片段的开始和结尾
逆置片段,从头遍历数组,判断是否为升序
#include <stdio.h>
#include <stdlib.h>
int find(int a[],int n)
{
int temp = 0;
int j = 0;
int first = 0, last = 0;
for(int i=0;i<n-1;i++)
{
if(a[i+1] < a[i])
{
first = i;
break;
}
}
for(int i=n-1;i>=0;i--)
{
if(a[i] < a[i-1])
{
last = i;
break;
}
}
//printf("%d%d",first, last);
for(int i = first;i<=(first+last)/2;i++)
{
temp = a[i];
a[i] = a[last+first-i];
a[last+first-i] = temp;
}
for(int i = 0;i<n-1;i++)
{
if(a[i]<a[i+1])
{
j++;
}
}
if(j==n-1)
{
printf("yes");
}
else{
printf("no");
}
}
int main()
{
int n;
scanf("%d\n",&n);
int a[n];
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}
find(a,n);
}