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

熱點內容
vb淘寶源碼 發布:2025-05-06 21:08:23 瀏覽:654
很多人的普通支付密碼是多少 發布:2025-05-06 21:08:22 瀏覽:659
統治戰場免費腳本 發布:2025-05-06 21:07:24 瀏覽:779
慶余年哪裡可以離線緩存 發布:2025-05-06 21:06:49 瀏覽:371
傳奇自動穿裝備腳本 發布:2025-05-06 20:54:06 瀏覽:265
網易沒有伺服器什麼梗 發布:2025-05-06 20:45:52 瀏覽:50
刪除qq群上傳的照片怎麼刪除不了怎麼辦 發布:2025-05-06 20:44:14 瀏覽:590
python用什麼開發工具 發布:2025-05-06 20:33:06 瀏覽:87
寬頻上傳提速 發布:2025-05-06 20:24:58 瀏覽:27
win搭建域名伺服器配置 發布:2025-05-06 20:16:49 瀏覽:784