codevs 1006 等差数列

                     题目描述 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<cstring>
  4 #include<algorithm>
  5 #define ll long long 
  6 
  7 using namespace std;
  8 const int N=110;
  9 
 10 ll a[N];
 11 
 12 inline ll read()
 13 {
 14     ll x=0;
 15     int f=1;
 16     char c=getchar();
 17     while(c<'0'||c>'9'){if(c=='-')f=-1;c=getchar();    }
 18     while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();
 19     return x*f;
 20 }
 21 
 22 int main()
 23 {
 24     ll n=read();
 25     ll minn=99999999,maxn=-1;
 26     for(int i=1;i<=n;i++)
 27     {
 28         a[i]=read();
 29         minn=min(minn,a[i]);
 30         maxn=max(maxn,a[i]);
 31     }
 32 
 33     sort(a+1,a+n+1);
 34     int _=a[2]-a[1];
 35     int answer=-1;
 36     for(int i=_;i<=maxn-minn;i++)
 37     {
 38         int ans=0;
 39         bool flag=1;
 40         for(int j=2;j<=n&&flag;j++)
 41         {
 42             if(a[j]-a[j-1]==i)
 43             {
 44                 ans++;
 45                 continue;
 46             }
 47             if(a[j]-a[j-1]>i)flag=0;
 48             else 
 49             {
 50                 int k=j+1;
 51                 while(a[k]-a[j-1]<i)
 52                     k++;
 53                 if(a[k]-a[j-1]==i)
 54                     ans++,j=k;
 55                 else 
 56                     flag=0;
 57             }
 58         }
 59         answer=max(answer,ans+1); 
 60     }
 61     printf("%d",answer);
 62     return 0;
 63 }
 64 /*
 65 45
 66 353351
 67 1248933
 68 360478
 69 3128604
 70 5075424
 71 289208
 72 324843
 73 7606292
 74 9152117
 75 282081
 76 6367622
 77 2451508
 78 438875
 79 7211874
 80 388986
 81 417494
 82 7960699
 83 381859
 84 6825135
 85 396113
 86 267827
 87 424621
 88 6256269
 89 6251132
 90 296335
 91 339097
 92 346224
 93 7140378
 94 6235384
 95 260700
 96 403240
 97 6329216
 98 303462
 99 119647
100 374732
101 410367
102 6406648
103 330709
104 814750
105 367605
106 274954
107 431748
108 310589
109 317716
110 331970
111 */
原文地址:https://www.cnblogs.com/lyqlyq/p/7056038.html