codevs 等差数列

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
【题目大意】
求最长等差数列
【思路】
暴力
【code】
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,cnt,cha,maxx,nxt,a[101];
int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
    scanf("%d",&a[i]);
    sort(a+1,a+n+1);
    for(int i=1;i<=n;i++)
    for(int j=i+1;j<=n;j++)
    {
        cnt=2;cha=a[j]-a[i];
        nxt=a[j]+cha;
        for(int k=j+1;k<=n;k++)
        {
            if(a[k]==nxt)
            {
                cnt++;
                nxt+=cha;
            }
        }
        maxx=cnt>maxx?cnt:maxx;
    }
    if(n==1)printf("%d
",1);
    else
    printf("%d",maxx);
    return 0;
}
原文地址:https://www.cnblogs.com/zzyh/p/7137795.html