【codevs 1006】等差数列

来源:互联网 发布:人类起源于中国知乎 编辑:程序博客网 时间:2024/05/24 06:55

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

数据范围及提示 Data Size & Hint

枚举公差即可~

#include <iostream>#include <cstdio>#include <cstring>#include <queue>#include <algorithm>#include <vector>using namespace std;int n,x,d;//公差;vector < int > s;int ans;int main(){    scanf("%d",&n);    for(int i = 1;i <= n;i ++)    {        scanf("%d",&x);        s.push_back(x);    }    sort(s.begin(),s.end());    for(int i = 0;i < n;i ++)    {        int sum = 1;        for(int j = i + 1; j < n;j ++)        {            if(j == i + 1)  d = s[j] - s[i];            if(s[j] - s[i] == d * sum)  sum ++;        }//以每一个数作为开头,枚举公差         ans = max(ans,sum);    }    printf("%d\n",ans);    return 0;}
0 0