Arithmetic Progression超时了n次,终于过了!

Arithmetic Progression
题目描述
“In mathematics, an arithmetic progression (AP) or arithmetic sequence is a sequence of numbers such that the difference between the consecutive terms is constant. For instance, the sequence 5, 7, 9, 11, 13, … is an arithmetic progression with common difference of 2.”
- Wikipedia


This is a quite simple problem, give you the sequence, you are supposed to find the length of the longest consecutive subsequence which is an arithmetic progression.
输入格式
There are several test cases. For each case there are two lines. The first line contains an integer number N (1 <= N <= 100000) indicating the number of the sequence. The following line describes the N integer numbers indicating the sequence, each number will fit in a 32bit signed integer.
输出
For each case, please output the answer in one line.
样例输入
6
1 4 7 9 11 14
样例输出
3

代码:

#include<stdio.h>

#define N 100005
int a[100001];
int max(int x,int y){
return(x>y?x:y);
}
int main()
{
int n,i;
while(scanf("%d",&n)!=EOF) 
{
for(i=0;i<n;i++) 
{
scanf("%d",&a[i]);
}
int m=1,j=0,k,s;
while(j<n-1) 
{
k=j+1; 
s=a[k] - a[j];
while(k<n&&(a[k]-a[k-1])==s) 
k++;
k--;
m=max(m, k-j+1);
j=k;
}
printf("%d\n",m);
}
return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

七刀

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值