题目2 : K-th string
时间限制:10000ms
单点时限:1000ms
内存限制:256MB
Description
Consider astring set that each of them consists of {0, 1} only. All strings in the sethave the same number of 0s and 1s. Write a program to find and output the K-thstring according to the dictionary order. If such a string doesn’t exist, orthe 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 lineof the input file contains a single integer t (1 ≤ t ≤ 10000), the number oftest 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 numberof ‘0’s, M stands for the number of ‘1’s, and K stands for the K-th of stringin 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”.
样例输入
3
2 2 2
2 2 7
4 7 47
样例输出
0101
Impossible
01010111011
Java程序源代码:
package com.luo.javawork.testdemo;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class KthString {
public static String[] combination(int m, int n) {
ArrayList<String> arrayList = new ArrayList<>();
char[] totalArray = new char[m];
for (int i = 0; i < m; i++) {
if (i < n)
totalArray[i] = '1';
else
totalArray[i] = '0';
}
arrayList.add(String.valueOf(totalArray));
int index = -1; // "10"反转置换法
while ((index = String.valueOf(totalArray).indexOf("10")) != -1) {
totalArray[index] = '0'; // 交换"10"为"01"
totalArray[index + 1] = '1';
// 计算刚反转的"10"前面所有的'1'全部移动到最左边
int count = 0;
for (int i = 0; i < index; i++) {
if (totalArray[i] == '1')
count++;
}
for (int j = 0; j < index; j++) {
if (j < count)
totalArray[j] = '1';
else
totalArray[j] = '0';
}
// 输出结果
arrayList.add(String.valueOf(totalArray));
}
String[] array = new String[arrayList.size()];
for (int i = 0; i < arrayList.size(); i++) {
array[i] = arrayList.get(i);
}
return array;
}
@SuppressWarnings("resource")
public static void main(String[] args) {
try {
Exception e = new Exception();
Scanner scanner = new Scanner(System.in);
int num = 0;
num = scanner.nextInt();
if (num < 1 || num > 10000) {
throw e;
}
String[] array = null;
ArrayList<String> result = new ArrayList<String>();
String str = "";
String[] string = new String[3];
int[][] arr = new int[num][3];
for (int i = 0; i < num; i++) {
scanner = new Scanner(System.in);
str = scanner.nextLine();
string = str.split(" ");
arr[i][0] = Integer.parseInt(string[0]);
arr[i][1] = Integer.parseInt(string[1]);
arr[i][2] = Integer.parseInt(string[2]);
if (arr[i][0] + arr[i][1] > 33 || arr[i][0] + arr[i][1] < 2
|| arr[i][0] < 0 || arr[i][1] < 0 || arr[i][2] < 1
|| arr[i][2] > 1000000000) {
throw e;
}
}
for (int i = 0; i < num; i++) {
// 生成组合数
array = combination(arr[i][0] + arr[i][1], arr[i][1]);
// 排序
Arrays.sort(array);
if (array.length >= arr[i][2]) {
result.add(array[arr[i][2] - 1]);
} else {
result.add("Impossible");
}
}
for (int i = 0; i < result.size(); i++) {
System.out.println(result.get(i));
}
} catch (Exception e) {
System.out.println("Impossible");
}
}
}