2 3 1 4 2
1 2 3 12 13 14 21 23 24 31 32 34 41 42 43
#include<iostream>
#include<string>
#include<algorithm> 
                 
using namespace 
  std;
int main()
{
	int 
  t;
	cin>>t;
	while(t--)
	{
int 
  n,m,i,k=0;
		cin>>n>>m;
		string 
  s1,s2;
		for(i=1;i<=n;i++)
		{
			s1+=i+‘0‘;
		}
		s2=s1.substr(0,m); 
                
    
		cout<<s2<<endl;
		while(next_permutation(s1.begin(),s1.end())) 
            
   //next_permutation(),所以必须用到#include<algorithm>头文件
{
			//cout<<"这是第"<<++k<<"次"<<endl;
			//cout<<"字符串s1 
  "<<s1<<endl;
			//cout<<"s2未处理 
  "<<s1.substr(0,m)<<endl;			
            
  if(s2!=s1.substr(0,m))
			{
				s2=s1.substr(0,m);
				cout<<s2<<endl;
			}
		}
	}
	return 
  0;
}
归纳知识点:
(1) substr(start,length) 返回一个从指定位置开始,并具有指定长度的子字符串。
参数:
start
必选。所需的子字符串的起始位置。字符串中第一个字符的索引为 
  0。
length
可选项。返回的子字符串中包含的字符数。
备注
如果 
  length 为 0 或负数,将返回一个空字符串。如果没有指定该参数,则子字符串将延续到字符串的结尾。
(2) next_permutation 这是一个求一个排序的下一个排列的函数,可以遍历全排列,要包含头文件<algorithm>,与之相反的是prev_permutation
int型的next_permutation ------- next_permutation(a,a+m) 备注:int a[3]; m必须小于等于3
char型的next_permutation-------next_permutation(first,last) 备注:char ch[200]; char*first=ch; char* last=ch+strlen(ch);
string 型的next_permutation------next_permutation(s.begin(),s.end()) 备注:string s;
原文:http://www.cnblogs.com/zdblog/p/3632691.html