#include<iostream>
#include<stack>
#include<vector>
#include<stdlib.h>//rand()
#include<algorithm>//find()
using namespace std;
class Solution{
public:
vector<vector<int>> GetRandomNumber(int max,int num){
vector<int> tmp;
vector<vector<int>> result;
while(1){
if(tmp.size() == max){
if(find(result.begin(),result.end(),tmp) == result.end()){
result.push_back(tmp);
if(result.size() == num)
break;
}
tmp.clear();
}
int number = rand()%max;
if(find(tmp.begin(),tmp.end(),number) == tmp.end()){
tmp.push_back(number);
}
}
return result;
}
};
int main(){
int max = 12;
int num = 24;
Solution tg;
vector<vector<int>> res;
res = tg.GetRandomNumber(max,num);
system("pause");
}
以下是Python代码实现:
<div> import random
result = []
tmp= []
while True:
if len(tmp)==12:
if tmp not in result:
result.append(tuple(tmp))
if(len(result) == 24):
print result
break
tmp = []
number = random.randint(1,12)
if number not in tmp:
tmp.append(number)
</div>