C/C++ solutions of CS interview questions
How would you choose m random numbers between 1-n
void genM(int m,int n) { int *x = new int[n+1]; for(i =0;i<=n;i++) x[i] = i; for(i = 1; i<=m; i++) { j = randint(i, n); swap(x[i],x[j]); } for(i = 1;i<=m;i++) std::cout << x[i]<< std::endl; }
No comments:
Post a Comment