[手游项目2]-12-min 到max随机n个不同的数

#include <Windows.h>
#include <vector>

int Rand(int from,int to)
{
	int  nDec, nMin;

	if (from > to)
	{
		nDec =  from - to + 1;
		nMin = to;
	}
	else
	{
		nDec =  to - from + 1;
		nMin = from;
	}

	int nRand = rand() % nDec + nMin;

	return nRand;
}

void MultRandom(std::vector<int>&tabRet, int nMin, int nMax, int nRandCount )
{
	int nTemp = nMax - nMin + 1;
	if (nTemp < nRandCount ||  nRandCount <= 0)
	{
		return ;
	}
	tabRet.clear();
	std::vector<int>tabTemp(nTemp);
	int nTop = nMax;
	for (int i = 1; i<= nRandCount; i++)
	{
		int nRandResult = Rand(nMin, nTop);
		int nResult = 0;
		if(tabTemp[nRandResult - nMin] <= nMin)
		{
			nResult = nRandResult;
		}
		else
		{
			nResult = tabTemp[nRandResult - nMin];
		}
		tabRet.push_back(nResult);
		if (nRandResult != nTop )
		{
			if(tabTemp[nTop - nMin] <  nMin)
			{
				tabTemp[nRandResult - nMin] = nTop;
			}
			else
			{
				tabTemp[nRandResult - nMin] = tabTemp[nTop - nMin];
			}
		}
		nTop--;
	}
}


int main()
{
	std::vector<int>tabRet;a
	MultRandom(tabRet,1,100,100);
	system("pause");
}

猜你喜欢

转载自blog.csdn.net/q277055799/article/details/82220382
今日推荐