#include <iostream> #include <vector> using namespace std; void HeapPermutation(vector<int>& nums, int size) { if (size == 1) { for (int num : nums) cout << num << " "; cout << endl; return; } for (int i = 0; i < size; i++) { HeapPermutation(nums, size - 1); if (size % 2 == 1) swap(nums[0], nums[size - 1]); else swap(nums[i], nums[size - 1]); } } int main() { int n; cin >> n; vector<int> nums(n); for (int i = 0; i < n; i++) cin >> nums[i]; HeapPermutation(nums, n); return 0; }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter