#include <stdio.h>
#include <string.h>
#include <iostream>
#include <map>
using namespace std;
typedef map<int,int> TempList;
int GetNumNum(int N,int M) //N个数中取M个数多少种情况 自己写
{
return 1;
}
int GNumNumNum(int nLen) //N个数种取3,4,5...N个数
{
int nNum = 0;
for(int i = 3;i<=nLen;++i) //i初始为3意思是等差数列最少要三个数吧?
{
nNum+= GetNumNum(nLen,i);
}
return nNum;
}
int GetNum(int nArray[],int nLen)
{
TempList GradeList;
for (int i = 0;i<nLen-1;++i)
{
for(int j = 1;j<nLen;++j)
{
int nGrade = nArray[j]-nArray[i];
if(GradeList.find(nGrade)==GradeList.end())
{
GradeList[nGrade] = 1;
}
else
{
GradeList[nGrade]++;
}
}
}
int nNum = 0;
TempList::iterator itr = GradeList.begin();
for(itr;itr!=GradeList.end();++itr)
{
if(3<itr->second)
{
nNum+= GNumNumNum(itr->second);
}
}
return nNum;
}
void main()
{
const int nLen = 10;
int nArray[nLen] = {1,2,3,4,5,6,7,8,9,10};
cout<<GetNum(nArray,nLen);
system("\npause\n");
}
连续的情况
local nArray = {}
for i = 1,100 do
local nRand = math.random(1,100)
if(nArray[nRand] == nil)then
table.insert(nArray,nRand,nRand)
end
end
function GetNum(DataList)
local nCount = 0
for k,v in pairs(DataList)do
nCount = nCount +1
end
return nCount
end
function GetNext(nData)
local bFind = false
for k,v in pairs(nArray)do
if(bFind == true)then
return v
end
if(v == nData)then
bFind = true
end
end
return nil
end
function PrintList(DataList)
local strArray = ""
for k,v in pairs(DataList)do
strArray = strArray..tostring(v)..","
end
strArray = strArray.."\n"
print(strArray)
end
PrintList(nArray)
local nDValue = nArray[2] - nArray[1]
local nTmpList = {}
local nNum = 0
local nLen = #nArray
for k,v in pairs(nArray) do
local nPostFront = v
local nPostAfter = GetNext(v)
if(nPostAfter~=nil)then
local nNowDValue = nPostAfter - nPostFront
if(nDValue == nNowDValue)then
table.insert(nTmpList,nPostFront)
else
table.insert(nTmpList,nPostFront)
local N = GetNum(nTmpList)
if(3 <= N )then
nNum = nNum +1
PrintList(nTmpList)
end
nDValue = nNowDValue
nTmpList = {}
table.insert(nTmpList,nPostFront)
end
end
end
--print(nNum)