當前位置:首頁 » 操作系統 » M合成演算法

M合成演算法

發布時間: 2025-02-08 11:26:22

❶ 組合演算法:從m個數中選n個數的所有組合

#include <iostream.h>

int combine(int a[], int n, int m)
{
m = m > n ? n : m;

int* order = new int[m+1];
for(int i=0; i<=m; i++)
order[i] = i-1;

int count = 0;
int k = m;
bool flag = true;
while(order[0] == -1)
{
if(flag)
{
for(i=1; i<=m; i++)
cout << a[order[i]] << " ";
cout << endl;
count++;
flag = false;
}

order[k]++;
if(order[k] == n)
{
order[k--] = 0;
continue;
}

if(k < m)
{
order[++k] = order[k-1];
continue;
}

if(k == m)
flag = true;
}

delete[] order;
return count;
}

int main()
{
const int M = 4;
const int N = 3;
int a[M];
int b[N];
for(int i=0;i<M;i++)
a[i] = i+1;

combine(a,M,N);

return 0;
}

熱點內容
懶人學編程 發布:2025-07-11 09:44:58 瀏覽:333
android命令行簽名 發布:2025-07-11 09:44:15 瀏覽:881
應用密碼怎麼取消密碼 發布:2025-07-11 09:28:33 瀏覽:797
pythonubuntumysql 發布:2025-07-11 09:23:47 瀏覽:531
硬碟屬於外部存儲器嗎 發布:2025-07-11 09:09:39 瀏覽:936
vs源碼查看 發布:2025-07-11 09:06:43 瀏覽:971
ip當前伺服器不可用是什麼意思 發布:2025-07-11 08:57:55 瀏覽:335
acfun如何緩存 發布:2025-07-11 08:48:12 瀏覽:3
我的世界伺服器tag 發布:2025-07-11 08:48:09 瀏覽:891
c語言設置 發布:2025-07-11 08:48:02 瀏覽:149