C 生成列表的不同排列
C++ generating different permutations of a list?
嗨,我正在为C 做一个作业,不幸的是,我的老师无法教我有关C 的任何东西,因此我必须求助于互联网以帮助我。
我会感谢一种简单,简单的方法来输出列表的所有排列,例如,如果列表x具有[1,2,3]的3个不同值,则输出= [1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]等...该列表不一定具有为了按此顺序,只需在列表中输出1、2、3的所有不同组合即可。就这样。我在网上浏览了如何弄清楚如何做到这一点,没有太多运气的了解。我已经看到人们为此使用向量,还有很多我对0%的理解的其他事物。我很想复制并粘贴他人的代码,但这无济于事,因为我是唯一一个自愿学习这种语言的C 的任何地方。因此,请尝试尽可能多地用外行的术语来解释它。如果有帮助,我正在使用C 11。
这是我自己的
std::cout << "nPart 2: Enter a list of 3 numbers, press enter after each value: n";
std::list<int> mylist1;
int v1;
int v2;
int v3;
std::cin >> v1 >> v2 >> v3; //input that will go into the list
mylist1.push_front(v1);
mylist1.push_back(v2);
mylist1.push_back(v3);
有一个函数,称为 next_permutation
。
#include <algorithm>
#include <vector>
#include <iostream>
void show(const std::vector<int> &vtr) {
for (auto val : vtr) std::cout << val << " ";
std::cout << "n";
}
int main() {
std::vector<int> vec{ 12,1,6,4,0 };
std::sort(vec.begin(), vec.end());
show(vec);
while (std::next_permutation(vec.begin(), vec.end())) {
show(vec);
}
}
相关文章:
- 比较并显示使用最小值(a,b)和最大值(a、b)升序排列的4个数字
- Pybind11:将元组列表从Python传递到C++
- 从链接列表c++中删除一个项目
- 如何(从固定列表中)选择一个数字序列,该序列将与目标数字相加
- 为什么不;名字在地图上是按顺序排列的吗
- C++如何通过用户输入删除列表元素
- 读取文件的最后一行并输入到链接列表时出错
- 在数组中输入 n 个整数的列表,并以类似于钟摆来回移动的方式排列它们. 输入-1 3 2 5 4,输出5 3 1 2 4
- 最大数量.给定一个非负整数列表,排列它们以使它们形成最大的数字
- 添加气泡排序代码以排列列表
- C 生成列表的不同排列
- 获得给定向量的排列索引列表的最佳方法是什么?
- 我可以使用普通的气泡排序来按字母顺序排列列表吗?
- 如何在C++中随机排列指针列表
- 如何在C++上按字母顺序排列列表
- 在用户完成拖放后重新排列多个选定的列表视图项
- 如何在boost::邻接列表中排列节点
- 计算包含某些项的列表集合的唯一排列的算法
- 我正在创建一个双链接列表,它将按字母顺序排列姓名列表,但我不确定在int main()函数中放入什么
- 使函数接受其参数列表的任意排列的规范方法是什么?