擅长排列的小明

地址:http://acm.nyist.net/JudgeOnline/problem.php?pid=19

擅长排列的小明

时间限制: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

思路:题目很明确,知识点就是字典序的全排列。关键就是只是一共有n个,但是只是输出m个,即:输出其中几个,怎么让重复的不输出来。

趁机会,复习+学习下全排列,全组合(递归),字典序的全排列(非递归)

这两个微博关于这几个知识点写的很好,分享下:

http://www.cnblogs.com/lifegoesonitself/p/3225803.html

http://www.cnblogs.com/pmars/p/3458289.html

最后附上代码,不过却是wrong answer,是参考讨论区的,都不对,难道C++行,java不行??

import java.util.Arrays;
import java.util.Scanner;
public class Main {
	static int pre=0;	//进行标识前一个,避免了重复 
	
	public static void main(String[] args){
		Scanner sc=new Scanner(System.in);
		int N=sc.nextInt();
		while(N-->0){
			int n=sc.nextInt();
			int m=sc.nextInt();
			int [] chs=new int[n];
			for(int i=0;i<n;i++){
				chs[i]=i+1;
			}
			permutationList(chs, n, m);
		}
	}

	 private static void arraysSort(int [] chs,int fromIndex, int endIndex){
         Arrays.sort(chs, fromIndex, endIndex - fromIndex + 1);
     }
	public static void swap(int [] chs,int a,int b){
		if(a!=b){
			int tmp=chs[a];
			chs[a]=chs[b];
			chs[b]=tmp;
		}
	}
	//字典序实现全排列,非递归
	public static void permutationList(int[] chs,int n,int m){
		int fromIndex,endIndex,changeIndex;
		arraysSort(chs,0,n-1);	//从小到大的排列,为了取到最小的
		do{
			// 输出一种全排列
			outPut(chs,m);
			fromIndex=endIndex=n-1;
			// 向前查找第一个变小的元素
			while(fromIndex>0&&chs[fromIndex]<chs[fromIndex-1])
				fromIndex--;
			changeIndex=fromIndex;	//为了比较少一个
			if(fromIndex==0)	//找不到较小的,即:倒序
				break;
			// 向后查找最后一个大于words[fromIndex-1]的元素,为了比较少一个
			while(changeIndex+1<n&&chs[changeIndex+1]>chs[fromIndex-1])
				changeIndex++;
			swap(chs, fromIndex-1, changeIndex);	// 交换两个值
			invertArray(chs, fromIndex, endIndex);	// 对后面的所有值进行反向处理
			
			
		}while(true);
	}
	//进行翻转
	private static void invertArray(int[] chs,int fromIndex, int endIndex){
         for (; fromIndex < endIndex; ++fromIndex, --endIndex)
             swap(chs,fromIndex, endIndex);
    }
	//输出需要的那部分
	public static void outPut(int[] chs,int m){
		if(chs[m-1]!=pre){	//存储前一个值,避免重复的值
		for(int i=0;i<m;i++)
			System.out.print(chs[i]);
		System.out.println();
		pre=chs[m-1];
		}
	}
}

再附上C++的。用上特殊的全排列函数,直接调用就行了。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,list[10];
int main()
{
int T,i,pre;
scanf("%d",&T);
while(T--)
{
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
list[i]=i+1;
pre=0;
do 
{
if(list[m-1]!=pre)
{
for(i=0;i<m;i++)
cout<<list[i];
cout<<endl;
pre=list[m-1];
}
}while (next_permutation(list,list+n));
}
return 0;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值