日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++实现全排列

發(fā)布時(shí)間:2025/3/15 c/c++ 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++实现全排列 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#include <iostream> #include<vector> #include<string> #include <unordered_map> #include <unordered_set> #include <map> #include <queue> #include <algorithm>//算法頭文件 #include <numeric> #include <stack> #include<typeinfo> #include<regex> #include<stdio.h> #include <stdlib.h> #include <crtdbg.h> #include <thread> //#include<bits/stdc++.h> using namespace std;void Perm(int start, int end, vector<int>& arr); int main() {int n;cout << "請(qǐng)輸入有多少個(gè)元素:";cin >> n;int a;vector<int> arr;for (int i = 0; i < n; i++){cin >> a;arr.push_back(a);}Perm(0, n, arr);return 0; } void Perm(int start, int end, vector<int>& arr) //這里相當(dāng)于遞歸算法, {//道理start=end時(shí),就相當(dāng)于這次遞歸結(jié)束,找到了一個(gè)排列組合if (start == end){for (int i = 0; i < end; i++){cout << arr[i] << " ";}cout << endl;return;}for (int i = start; i < end; i++){swap(arr[start], arr[i]);//假設(shè)3,4,5為例,3P(4,5)是已經(jīng)一個(gè)組合,后面則是4P(3,5),5P(3,4),因此需要交換一下Perm(start + 1, end, arr);//相當(dāng)于P(4,5)swap(arr[i], arr[start]);//回溯,把順序還原,不然就破壞了數(shù)據(jù),排列就進(jìn)行不下去了} }

總結(jié)

以上是生活随笔為你收集整理的C++实现全排列的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。