首页 > 代码库 > 算法1

算法1

从一堆数中随机取出一些值的方法:

#include <sstream>#include <iostream>#include <vector>#include <stdlib.h>using namespace std;int random(int min,int max){    return rand() % (max -min + 1) + min;}int main(){    int len = 20;    vector<int> vect;    for(int i = 10; i < len + 10; i++)        vect.push_back(i);    int array[15];    for(int i = 0; i < 15; i++)    {        int num = random(0,len - i -1);        array[i] = vect[num];        vect[num] = vect[len -i -1];    }    for(auto& i : array)        cout<<i<<endl;}

 

算法1