codevs 1006 等差数列 题解报告

3 篇文章 0 订阅
2 篇文章 0 订阅

噫,打算去一个一个刷codevs题库。。

先上题目;
题目描述 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

嗯,,乍一看
嗨呀好麻烦啊。
其实
想一想。。
数据范围辣么小,暴力就好了~

首先肯定要对数据进行排序;
之后,枚举每一个点和它之后的一个点。
二者之差就是公差
然后往后一个一个找,每次加上公差,维护最长的值。
即可;

代码::!:~::~

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<stack>
#include<cstdlib>
#include<string>
#include<bitset>
#include<iomanip>
#include<deque>
#define INF 1000000000
#define fi first
#define se second
#define N 100005
#define P 1000000007
#define debug(x) cerr<<#x<<"="<<x<<endl
#define MP(x,y) make_pair(x,y)
using namespace std;
int n,m,a[101],su=0;
inline int get_num()
{
int num = 0;
char c;
bool flag = false;
while ((c = getchar()) == ' ' || c == '\n' || c == '\r');
if (c == '-') flag = true;
else num = c - '0';
while (isdigit(c = getchar()))
num = num * 10 + c - '0';
return (flag ? -1 : 1) * num;
} //忽略这一堆没用的东西
int main()
{
    cin>>n;
    for(int i=1;i<=n;i++)
    cin>>a[i];
    sort(a+1,1+n+a);
    for(int i=1;i<=n;i++)
    {
        for(int j=i+1;j<=n;j++)
        {
            int ans=2;
            int gc=a[j]-a[i];
            int now=a[j]+gc;
            for(int k=j+1;k<=n;k++)
            {
                if(a[k]==now)
                {
                    ans++;
                    now+=gc;
                }
            }
            su=max(su,ans);
        }
    }
    cout<<su;

}

呜啦啦啦阿联~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值