#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;
}
//数组下标减掉min是为了减少tabTemp初始化的大小,节约内存,
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, nMax + 1);
int nTop = nMax;
for (int i = 1; i<= nRandCount; i++)
{
int nRandResult = Rand(nMin, nTop);
int nResult = 0;
if(tabTemp[nRandResult - nMin] <= nMax && nMin <= tabTemp[nRandResult - nMin])
{
nResult = tabTemp[nRandResult - nMin];
}
else
{
nResult = nRandResult;
}
tabRet.push_back(nResult);
if (nRandResult != nTop )
{
if(tabTemp[nTop - nMin] <= nMax && nMin <= tabTemp[nTop - nMin])
{
tabTemp[nRandResult - nMin] = tabTemp[nTop - nMin];
}
else
{
tabTemp[nRandResult - nMin] = nTop;
}
}
nTop--;
}
}
int main()
{
std::vector<int>tabRet;a
MultRandom(tabRet,1,100,100);
system("pause");
}