-
3 2 2 2 2 2 7 4 7 47
样例输出
-
0101 Impossible 01010111011
我的java代码。。。仅供参考,跪求大神指正!
-
package com.summer.intern;
import java.util.Scanner;
public class Main {
private static StringBuilder output = new StringBuilder("");
public static int simpleCircle(int num) {// 简单的循环计算的阶乘
int sum = 1;
for (int i = 1; i <= num; i++) {// 循环num
sum *= i;// 每循环一次进行乘法运算
}
return sum;// 返回阶乘的值
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int count = input.nextInt();
int loop = 0;
while (input.hasNextLine()) {
String line = input.nextLine();
if (line.length() > 1) {
String num[] = line.split(" ");
int num_0 = Integer.parseInt(num[0]);
int num_1 = Integer.parseInt(num[1]);
int k = Integer.parseInt(num[2]);
while (num_0 != 0 ||num_1 != 0) {
int max_num = simpleCircle(num_0 + num_1)/ (simpleCircle(num_0) * simpleCircle(num_1));
if(max_num==1)
{
for(int i=0;i<num_0;i++)
output.append("0");
for(int i=0;i<num_1;i++)
output.append("1");
break;
}
//System.out.println(max_num);
if (k > max_num) {
System.out.println("Impossible");
break;
} else {
int temp_num=simpleCircle(num_0-1 + num_1)/ (simpleCircle(num_0-1) * simpleCircle(num_1));
if (k > temp_num){
output.append("1");
num_1--;
k = k-temp_num;
} else {
output.append("0");
num_0--;
}
}
}
if(output.length()!=0)
{
System.out.println(output.toString());
}
output.delete(0, output.length());
}
if(loop==count) break;
loop++;
}
}
}
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 such 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”.