噫,打算去一个一个刷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;
}
呜啦啦啦阿联~