nyoj19(排列组合next_permutation(s.begin(),s.end()))
2014-11-02 20:14
232 查看
题目意思:
从n个数中选择m个数,按字典序输出其排列。
http://acm.nyist.net/JudgeOnline/problem.php?pid=19
例:
输入:n=3,m=1; 输出:1 2 3
输入:n=4,m=2; 输出:12 13 14 21 23 24 31 32 34 41 42 43
题目分析:
此题为全排列的前m个数,只需对n个数调用全排列函数next_permutation(),去除重复的输出前m个即可。
AC代码:
/**
*改写的全排列,这里用字符串输入,方便判断是否重复输出
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int n,m;
int main()
{
int t;
cin>>t;
while(t--){
cin>>n>>m;
string s,s1;
for(int i=1;i<=n;i++) s+=i+'0';
s1=s.substr(0,m);
cout<<s1<<endl;
while(next_permutation(s.begin(),s.end())){
if(s1!=s.substr(0,m)){//用来判断重复输出,否则会输出多次
s1=s.substr(0,m);
cout<<s1<<endl;
}
}
}
return 0;
}
从n个数中选择m个数,按字典序输出其排列。
http://acm.nyist.net/JudgeOnline/problem.php?pid=19
例:
输入:n=3,m=1; 输出:1 2 3
输入:n=4,m=2; 输出:12 13 14 21 23 24 31 32 34 41 42 43
题目分析:
此题为全排列的前m个数,只需对n个数调用全排列函数next_permutation(),去除重复的输出前m个即可。
AC代码:
/**
*改写的全排列,这里用字符串输入,方便判断是否重复输出
*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
int n,m;
int main()
{
int t;
cin>>t;
while(t--){
cin>>n>>m;
string s,s1;
for(int i=1;i<=n;i++) s+=i+'0';
s1=s.substr(0,m);
cout<<s1<<endl;
while(next_permutation(s.begin(),s.end())){
if(s1!=s.substr(0,m)){//用来判断重复输出,否则会输出多次
s1=s.substr(0,m);
cout<<s1<<endl;
}
}
}
return 0;
}
相关文章推荐
- nyoj19(排列组合next_permutation(s.begin(),s.end()))
- NYOJ 19 擅长排列的小明 (STL之next_permutation()的使用)
- NYOJ19 擅长排列的小明【next_permutation】
- NYOJ 19 擅长排列的小名 next_permutation()的用法
- NYOJ--19--next_permutation()--擅长排列的小明
- nyoj 19 擅长排列的小明(深搜,next_permutation)
- nyoj 19 擅长排列的小明 【next_permutation() 】
- NYOJ 19 擅长排列的小明(全排列<next_permutation>)
- STL_算法(17)_排列组合 next_permutation() perv_permutation()
- STL——字典排序问题next_permutation(a.begin(),a.end())
- NYOJ 366 D的小L(从小到大输出排列。STL,next_permutation()函数的学习)
- NYOJ366 D的小L 和 NYOJO32 组合数 和 NYOJ19 擅长排列的小明 和 NYOJ488 素数环 【递归】
- NYOJ 19 擅长排列的小明(dsf STL——next_permutation函数)
- nyoj19 排列及组合
- HDOJ 1716 排列2 (next_permutation或dfs)
- (排列生成算法)枚举排列的两种方法:递归枚举和next_permutation (使用多用next_permutation)
- STL中的全排列函数next_permutation
- 全排列算法 next_permutation() 暴力枚举 C++
- 擅长排列的小明(next_permutation)
- 排列组合之next_permutation函数的运用