1. 程式人生 > >NYOJ 19 擅長排列的小明 (STL之next_permutation()的使用)

NYOJ 19 擅長排列的小明 (STL之next_permutation()的使用)

擅長排列的小明

時間限制:1000 ms  |  記憶體限制:65535 KB 難度:4
描述
小明十分聰明,而且十分擅長排列計算。比如給小明一個數字5,他能立刻給出1-5按字典序的全排列,如果你想為難他,在這5個數字中選出幾個數字讓他繼續全排列,那麼你就錯了,他同樣的很擅長。現在需要你寫一個程式來驗證擅長排列的小明到底對不對。
輸入
第一行輸入整數N(1<N<10)表示多少組測試資料,
每組測試資料第一行兩個整數 n m (1<n<9,0<m<=n)
輸出
在1-n中選取m個字元進行全排列,按字典序全部輸出,每種排列佔一行,每組資料間不需分界。如樣例
樣例輸入
2
3 1
4 2
樣例輸出
1
2
3
12
13
14
21
23
24
31
32
34
41
42
43
 
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
	int T,n,m;
	cin>>T;
        while(T--)
	{	
                string a,a1;
		cin>>n>>m;
                for(int i=1;i<=n;i++)
		 a+=i+'0';
		 a1=a.substr(0,m);
		 cout<<a1<<endl;
		 while(next_permutation(a.begin(),a.end()))
		 {
		 	if(a1!=a.substr(0,m))
		 	{
		 	a1=a.substr(0,m);
		 	cout<<a1<<endl;	
			}
		 	
		 }
		
	}
	return 0;
}