#最长连续子序列:对于一个长度为 N 的数组,输出其连续的子序列及其起止
位置,并且找出长度最长的那个子序列,输出其长度。如
1 2 3 0 1 2 7 -1 -2 -1 0 1
输出:1 2 3 起 1 止 3;
0 1 2 起 4 止 6;
-2 -1 0 1 起 9 止 12;
最长连续子序列为:-2 -1 0 1,长度为:4.
#include<iostream>
using namespace std;
#define N 12
int main(){
int a[N]{1,2,3,0,1,2,7,-1,-2,-1,0,1};
int x,y,i,j,k,l,s;
i=0,s=0;
while (i<N)
{
x=0;
k=i;
while (a[k]+1==a[k+1])
{
x++;
k++;
}
if (s<x)
{
s=x;
y=i;
}
if (x>=1)
{
for (j=i;j<=k;j++)
{
printf("%d ",a[j]);
}
printf("起%d止%d\n",i+1,k+1);
}
i=k+1;
}
printf("最长连续子序列为:");
for(l=y;l<=y+s;l++){
printf("%d ",a[l]);
}
printf("长度为%d",s+1);
return 0;
}