the k-th string JAVA版


Time Limit: 10000ms
Case Time Limit: 1000ms
Memory Limit: 256MB

Description

Consider a string set that each of them consists of {0, 1} only. All strings in the set have the same number of 0s and 1s. Write a program to find and output the K-th string according to the dictionary order. If s​uch a string doesn’t exist, or the input is not valid, please output “Impossible”. For example, if we have two ‘0’s and two ‘1’s, we will have a set with 6 different strings, {0011, 0101, 0110, 1001, 1010, 1100}, and the 4th string is 1001.

Input

The first line of the input file contains a single integer t (1 ≤ t ≤ 10000), the number of test cases, followed by the input data for each test case. Each test case is 3 integers separated by blank space: N, M(2 <= N + M <= 33 and N , M >= 0), K(1 <= K <= 1000000000). N stands for the number of ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of string in the set that needs to be printed as output.

Output

For each case, print exactly one line. If the string exists, please print it, otherwise print “Impossible”.

Sample In

3 2 2 2 2 2 7 4 7 47

Sample Out

0101 Impossible 01010111011

JAVA 干啥都想写成通用方法~ 效率还很低




public class K2thString
{
	static int num = 1;
	final static String ZERO="0"; 
	final static String ONE="1"; 
	public static void main(String A[])
	{
		int[] input ={2,3,2};
		Order(input, 3);
		String t=gene(input);
		Permutation(0,t.toCharArray());
	}
	
	public static void Order(int a[], int n)
	{
		int count=0;
		//计算组合数
		count= recursionEnd(a[0]+a[1],Math.min(a[0], a[1]))/(recursion(Math.min(a[0], a[1])));
		if(count<a[2])
		{
			System.out.println("Impossible");
			return;
		}
		
		
	}
	public static String gene(int a[])
	{     
		int count0=a[0];
		int count1=a[1];
		String t="";
		for(int i=0;i<count0;i++)
		{
			t+=ZERO;
		}
		for(int i=0;i<count1;i++)
		{
			t+=ONE;
		}
		return t;
	}
	
	public  static int recursion(int i){      //----------递归方法主体
		if(i<0)                          //<0退出
			return -1;
		else if(i==0)                     //0的阶乘=1
			return 1;
		else                           //0继续递归
			return i*recursion(i-1);
	}
	
	public  static int recursionEnd(int i,int end){      //----------递归方法主体
		if((end--)==0)
			return 1;
		if(i<0)                          //<0退出
			return -1;
		else if(i==0)                     //0的阶乘=1
			return 1;
		else                           //0继续递归
			return i*recursionEnd(i-1,end);
	}

	//在[nBegin,nEnd)区间中是否有字符与下标为pEnd的字符相等
	public static boolean IsSwap(int pBegin , int pEnd ,char str[])
	{
		int p;
		for(p = pBegin ; p < pEnd ; p++)
		{
			if(str[p] ==str[pEnd]  )
				return false;
		}
		return true;
	}
	public static char[] swap(int a , int b ,char str[])
	{
		char p=str[a];
		str[a]=str[b];
		str[b]=p;
		return str;
	}
	public static void Permutation( int pBegin,char str[])
	{
		if(str.length == pBegin)
		{
			 System.out.println("第个"+num+++"排列\t :"+new String(str).toString());
		}
		else
		{
			for(int  pCh = pBegin;str.length !=pCh ; pCh++)   //第pBegin个数分别与它后面的数字交换就能得到新的排列   
			{
				if(IsSwap(pBegin ,pCh,str))
				{
					str =swap(pBegin , pCh,str);
					Permutation(pBegin + 1,str);
					str =swap(pBegin , pCh,str);
				}
			}
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值