當前位置:首頁 » 操作系統 » 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-04-23 01:04:10 瀏覽:586
我爸電腦配置給別人看沒什麼事吧 發布:2025-04-23 00:58:54 瀏覽:723
大學編程課程 發布:2025-04-23 00:48:55 瀏覽:469
伺服器的內網ip有什麼用 發布:2025-04-23 00:46:40 瀏覽:958
誅仙3需要什麼配置 發布:2025-04-23 00:29:49 瀏覽:665
什麼是編譯錯誤參數不可選 發布:2025-04-23 00:23:06 瀏覽:520
libx264編譯 發布:2025-04-23 00:13:37 瀏覽:222
access的web資料庫 發布:2025-04-23 00:08:29 瀏覽:46
安卓上面的谷歌搜索框怎麼去除 發布:2025-04-23 00:07:27 瀏覽:171
c判斷文件夾是否存在 發布:2025-04-22 23:56:36 瀏覽:944