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

熱點內容
安卓源碼如何變成ios 發布:2025-07-11 08:20:35 瀏覽:624
純油雅閣配置怎麼選 發布:2025-07-11 08:16:37 瀏覽:319
數控圓孤編程 發布:2025-07-11 08:13:08 瀏覽:256
超級訪問羅大佑 發布:2025-07-11 07:43:33 瀏覽:387
邁騰有什麼安全配置 發布:2025-07-11 07:42:40 瀏覽:644
c語言字元逆序 發布:2025-07-11 07:41:57 瀏覽:923
怎麼配置交換機的console密碼 發布:2025-07-11 07:41:57 瀏覽:4
東芝存儲卡視頻 發布:2025-07-11 07:41:55 瀏覽:541
cs16為什麼搜不到區域網伺服器 發布:2025-07-11 07:41:21 瀏覽:913
php項目如何上傳伺服器 發布:2025-07-11 07:35:35 瀏覽:182