数组全排列情况

阅读量: 31 编辑

New wiki page content1607998606671904## 数组全排列情况

数组 {1,2,3} ,得到所有的排列情况,比如 1 2 3 或 3 2 1 或 2 1 3

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a[3] = {1, 2, 3};
    vector<int> nums(a, a + 3);

    // 排序数组,确保初始排列是最小的排列
    sort(nums.begin(), nums.end());

    int cnt = 0; //数量
    do {
        cnt++;
        for (int i = 0; i < nums.size(); i++) {
            cout << nums[i] << " ";
        }
        cout << endl;
    } while (next_permutation(nums.begin(), nums.end()));

    cout << cnt << endl;
    return 0;
}

爱码岛编程公众号
试卷资料
爱码岛编程小程序
在线刷题
苏ICP备13052010号
©2023 南京匠成信息科技有限公司