数组全排列 knuth 分解质因数
template<typename T> void swap(T* a, T* b) { T temp = *a; *a = *b; *b = temp; } //数组的全排列 void perm(int list[], int k, int m) { if (k==m) { copy(list,list+m,ostream_iterator<int>(cout," ")); cout<<endl; return; } for (int i=k; i<m; i++) { if(list[k] == list[i] && k != i) continue; swap(&list[k],&list[i]); perm(list,k+1,m); swap(&list[k],&list[i]); } }
void knuth(int n, int m)
{
srand((unsigned int)time(0));
for (int i=0; i<n; i++)
{
if (rand()%(n-i)<m)
{
cout<<i<<endl;
--m;
}
}
}
以下prim函数的功能是分解质因数。请填空
void prim(int m, int n)
{
if (m>n)
{
while ( ) n++;
;
prim(m,n);
cout<<n<<endl;
}
}
分别为:m%n 和 m/=n