您的位置:首页 > 其它

NYOJ 19 擅长排列的小名 next_permutation()的用法

2016-12-06 19:36 295 查看
主要就是next_permutation()这个函数的使用,没啥技术含量。
next_permutation()这使用时一定要保证参数是升序的,prev_permutation()与之相反否则生成的排列不全。具体原因看博客http://leonard1853.iteye.com/blog/1450085

本题代码

#include<iostream>
#include<algorithm>
#include<string>
using namespace std;

int main()
{
int t, i;
cin>>t;
while(t--)
{
int x, y;
string s1, s2;
cin>>x>>y;
for(i = 1; i <= x;i++)
{
s1 += '0' + i;
}//这里已经是升序了,所以不用再次排序。
s2 = s1.substr(0,y);//复制子串,即复制s1的前y个。
cout<<s2<<endl;
while(next_permutation(s1.begin(),s1.end()))//参数要是升序的。
{
if(s2 != s1.substr(0,y))
{
s2 = s1.substr(0,y);
cout<<s2<<endl;
}
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: