算法质疑 [英] An algorithm questrion

查看:94
本文介绍了算法质疑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一个关于如何迭代所有排序情况的问题.

假设有4个数字:1、2、3、4,以及如何显示使用C ++函数f(x)进行排序的所有情况,其中x是数字的计数.

例如:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

There is an problem about how to iterate all the situations of sorting.

Suppose there are 4 numbers:1,2,3,4, and how to display all the situations of sorting using C++ function , f(x) where x is the count of numbers.

example:
1234
1243
1324
1342
1423
1432
2134
2143
2314
2341
2413
2431
3124
3142
3214
3241
3412
3421
4123
4132
4213
4231
4312
4321

推荐答案

该算法已在STL中提供.
该函数称为std::next_permutation,可在<algorithm>标头中使用.

创建一个vector,然后将数字1、2、3和4推回vector.
循环调用next_permutation,直到它以vector::begin()vector::end()方法作为参数返回false.
The algorithm for this is already available in STL.
The function is called std::next_permutation and is available in the <algorithm> header.

Create a vector and push back the digits 1, 2, 3 and 4 into the vector.
Call next_permutation in a loop till it returns false with the vector::begin() and vector::end() methods as parameters.


这篇关于算法质疑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆