Arithmetic Progression

“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

6

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;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值