设为首页 加入收藏

TOP

C++中获取所有可能的组合
2013-11-20 14:23:32 来源: 作者: 【 】 浏览:109
Tags:获取 所有 可能 组合
    今天我们要实现的就是在C++中获取所有可能的组合,还是直接上代码吧!
    01.// zuhe.cpp : 定义控制台应用程序的入口点。
    02.//
    03.
    04.#include "stdio.h"
    05.#include "stdlib.h"
    06.#include "iostream"
    07.#include "vector"
    08.
    09.using namespace std;
    10.
    11.vector<int> team;
    12.vector<vector<int》 Teams;
    13.
    14.void getAll(int * arr, int i, int k,int n);
    15.
    16.int main(int argc, char * argv[])
    17.{
    18.    int arr[] = {1,2,3,4};
    19.    //getAll(arr, 0, 2, 4);
    20.
    21.    int n = 4;
    22.    for(int k = 1; k <= 4; k++)
    23.    {
    24.        getAll(arr, 0, k, n);
    25.    }
    26.
    27.    system("pause");
    28.    return 0;
    29.}
    30.
    31.void getAll(int * arr, int i, int k,int n)
    32.{
    33.    if(team.size() == k)
    34.    {
    35.        Teams.push_back(team);
    36.        return;
    37.    }
    38.
    39.    for(int j = i; j < n; j++)
    40.    {
    41.        team.push_back(arr[j]);
    42.        getAll(arr, j + 1, k, n);
    43.        team.pop_back();
    44.    }
    45.}
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇C++中实现最大回文子串 下一篇C++中字符串两次翻转

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·switch520最新的地址 (2025-12-24 19:19:41)
·微信聊天功能使用了 (2025-12-24 19:19:39)
·websocket和普通的so (2025-12-24 19:19:36)
·Python中文网 - 人生 (2025-12-24 18:49:47)
·【整整648集】这绝对 (2025-12-24 18:49:44)