题目
答案
#include<stdio.h>
int main()
{
int n;
scanf("%d",&n);
int a[n],i;
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int count=0,max=0,flag=0,position=0;
for(i=0;i<n;i++)
{
if(count==0)
count++;
else if(count>0)
{
if(a[i]>a[i-1]) count++;
else
{
if(count>max)
{
position=i-count;
max=count;
}
count=0;
i--;
}
}
if(i==n-1)
{
if(count>max)
{
position=i-count+1;
max=count;
}
}
}
printf("%d",a[position]);
for(i=position+1;i<position+max;i++)
printf(" %d",a[i]);
}
注意
遍历到数组的最后一个元素后,要做最后的处理;如果递增子序列的最后一个元素恰巧也是数组的最后一个元素,position
在i-count
的基础上还要加1