一个大小为N(0<N<1000)的整数数组, 求该数组的子数组(长度大于2)为等差数列的个数?...

#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)


 

 

转载于:https://www.cnblogs.com/byfei/p/6389901.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值