产生1-num的随机数,总共产生num组,C++,Python
来源:互联网 发布:360赚钱软件 编辑:程序博客网 时间:2024/06/05 20:20
#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 randomresult = []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>
0 0
- 产生1-num的随机数,总共产生num组,C++,Python
- python随机数的产生
- python num
- python产生随机数的方法
- C语言随机数的产生
- C#中随机数的产生
- C语言随机数的产生
- C/C++随机数的产生
- C语言随机数的产生
- --num 与 num-- 的陷阱
- c++/c 产生随机数
- python 产生随机数示例
- Python产生随机数
- Python产生随机数(转)
- python实例:产生随机数
- Python产生随机数
- python产生随机数
- [python]产生随机数
- 黑马程序员-C语言学习笔记-----内存分配函数(malloc、calloc、realloc)
- Ant学习笔记--入门
- cocos2d-x 3.4 之 文字按钮与图片按钮(更新I)
- CLOCK_MONOTONIC and CLOCK_REALTIME .
- Masonry和scrollView的结合使用,在手机横屏的状态也能看到竖屏时的内容
- 产生1-num的随机数,总共产生num组,C++,Python
- DrawerLayout实现侧滑
- groovy脚本导致的FullGC问题
- delphi获取目录下所有文件名[delphi]
- 1,Linux入门
- hdu 1505 City Game 最大矩形面积 单调队列
- java.lang.NoClassDefFoundError: org/apache/juli/logging/LogFactory的解决
- 焦点处理的先后顺序
- 前端能力模型-CSRF漏洞