當前位置:首頁 » 操作系統 » 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;
}

熱點內容
apmserv資料庫 發布:2025-10-17 13:26:09 瀏覽:478
dota2機器人腳本哪個適合新手 發布:2025-10-17 13:25:57 瀏覽:182
linux重命名文件的命令 發布:2025-10-17 13:00:40 瀏覽:463
python飛機 發布:2025-10-17 13:00:38 瀏覽:718
普通電腦支持伺服器硬碟 發布:2025-10-17 12:52:54 瀏覽:502
hdpiandroid 發布:2025-10-17 12:51:23 瀏覽:281
配置高的東西有哪些 發布:2025-10-17 12:48:57 瀏覽:358
老安卓手機用什麼版本微信 發布:2025-10-17 12:39:45 瀏覽:394
四軸A軸編程 發布:2025-10-17 12:39:04 瀏覽:604
java關閉的連接 發布:2025-10-17 12:34:53 瀏覽:310