zoukankan      html  css  js  c++  java
  • 一个大小为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("
    pause
    ");
    }


    连续的情况

    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.."
    "
    	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)
    


     

  • 相关阅读:
    目标检测 anchor 理解笔记
    目标检测 IOU(交并比) 理解笔记
    目标检测 非极大值抑制(Non-Maximum Suppression,NMS)
    c# 获取当前时间的微秒
    [macOS开发.NET Core] 一个简单的WEB程序
    海康相机SDK二次开发只有视频无声音问题
    [macOS开发.NET Core] 开篇 & 抉择 & 先利其器
    Linux学习--4.用户和组的管理
    Linux学习--3.命令及查看命令帮助
    Linux学习--2.文件管理的基本命令
  • 原文地址:https://www.cnblogs.com/byfei/p/6389901.html
Copyright © 2011-2022 走看看