“In mathematics, an arithmeticprogression (AP) or arithmetic sequence is a sequence of numbers such that thedifference between the consecutive terms is constant. For instance, thesequence 5, 7, 9, 11, 13, … is an arithmetic progression with common differenceof 2.”
- Wikipedia
Thisis a quite simple problem, give you the sequence, you are supposed to find the length of the longest consecutive subsequence which is an arithmeticprogression.
Input
Thereare several test cases. For each case there are two lines. The first linecontains an integer number N (1 <= N <= 100000) indicating the number ofthe sequence. The following line describes the N integer numbers indicating thesequence, each number will fit in a 32bit signed integer.
Output
Foreach case, please output the answer in one line.
Sample Input
1 4 7 9 11 14
Sample Output
3
试题分析:
水题,就是找最长的等差子序列,直接枚举就行了。下面是代码:
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N,i,j,k,max1;
int arr[100000];
while(scanf("%d",&N)!=EOF)
{
int q=0,length[10000]={0};
for(i=0;i<N;i++)
{
scanf("%d",&arr[i]);
}
for(j=1;j<N-1;j++)
{
if(arr[j+1]-arr[j]==arr[j]-arr[j-1])
{
length[q]++;
}
else
q++;
}
max1=length[0];
for(k=0;k<q;k++)
{
if(max1>length[k+1])
continue;
else
max1=length[k+1];
}
printf("%d\n",max1+2);
}
return 0;
}