日期:2014-05-20 浏览次数:21059 次
#include "stdio.h" #include <map> using namespace std; #define MAX_LEN 1000 int data[MAX_LEN]; int length; int best; int bestdelta; int beststart; void Run() { map<int,int> alldata; map<int,int> useddelta; scanf("%d",&length); for(int i = 0;i < length;i++) { scanf("%d",data+i); alldata[data[i]] = 0; } if(length == 1) { printf("%d\n",data[0]); return; } best = 2; bestdelta = data[1]-data[0]; beststart = data[0]; for(int i = 0;i < length-best+1;i++) { useddelta.clear(); for(int j = 0;j <= i;j++)useddelta[data[i]-data[j]] = 0; for(int j = i+1;j < length-best+2;j++) { if(useddelta.find(data[j]-data[i]) != useddelta.end())continue; int delta = data[j]-data[i]; int curlen = 2; int curdata = data[j]; while(true) { if(alldata.find(curdata+delta) != alldata.end()) { curlen++; curdata += delta; } else break; } if(curlen > best) { best = curlen; bestdelta = delta; beststart = data[i]; } } } for(int i = 0;i < best-1;i++,beststart += bestdelta)printf("%d ",beststart); printf("%d\n",beststart); } int main() { Run(); return 0; }
------解决方案--------------------
static void Main(string[] args) { List<NumListHeader> allList = new List<NumListHeader>(); string[] s = Console.ReadLine().Split(' '); NumListBody max = null; int[] allNum = s.ToList().ConvertAll<int>((x) => { return int.Parse(x); }).ToArray(); allNum.ToList().ForEach((n)=>{ allList.ForEach((l) => { bool newlist = l.bodys.Count==0; l.bodys.ForEach((b) => { if (n == b.Step * b.length + l.Start) { b.length++; if (max == null || b.length > max.length) { max = b; } } else if (n > b.Step * b.length + l.Start) { l.bodys.Remove(b); } else { newlist = true; } }); if (newlist) { l.bodys.Add(new NumListBody() { length =2, Step = n-l.Start, Start = l.Start }); } }); allList.Add(new NumListHeader() { Start = n }); }); Console.WriteLine("start:{0},step:{1},length:{2}", max.Start, max.Step, max.length); Console.Read(); } } class NumListBody { public int Start; public int Step; public int length; } class NumListHeader { public NumListHeader() { bodys = new List<NumListBody>(); } public int Start; public List<NumListBody> bodys; }