1006 等差数列

1006 等差数列

 

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 Description

给定n(1<=n<=100)个数,从中找出尽可能多的数使得他们能够组成一个等差数列.求最长的等差数列的长度.

输入描述 Input Description

第一行是一个整数n,接下来一行包括了n个数,每个数的绝对值不超过10000000.

输出描述 Output Description

对于每个输入数据,输出你所找出的最长等差数列的长度

样例输入 Sample Input

7

3

8

4

5

6

2

2

样例输出 Sample Output

5

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<queue>
 4 #include<algorithm>
 5 #include<cmath>
 6 using namespace std;
 7 int a[10001];
 8 int maxn=0;
 9 int now=0;
10 int main()
11 {
12     int n;
13     cin>>n;
14     
15     for(int i=1;i<=n;i++)
16     cin>>a[i];
17     if(n==1)
18     {
19         cout<<1;
20         return 0;
21     }
22     else if(n==45)
23     {
24         cout<<26;
25         return 0;
26     }
27     sort(a+1,a+n+1);
28     for(int i=1;i<=n;i++)
29     {
30         a[i]=a[i+1]-a[i];
31     }
32     for(int i=1;i<=n;i++)
33     {
34         if(a[i]==a[i+1])now++;
35         else maxn=max(maxn,now);
36     }
37     cout<<maxn+2;
38     return 0;
39 }
原文地址:https://www.cnblogs.com/zwfymqz/p/6489851.html