题目描述
一只兔子藏身于20个圆形排列的洞中(洞从1开始编号),一只狼从x号洞开始找,下次隔一个洞找(及在x+2号洞找),在下次个两个洞找(及在x+5号洞找),它找了n次仍然没有找到。问兔子可能在那些洞中。
输入描述:
输入有多组数据,每组数据一行两个整数分别为x和n(x <= 20,n <= 100000)
输出描述:
每组数据一行按从小到大的顺序输出兔子可能在的洞,数字之间用空格隔开。若每个洞都不肯能藏着兔子,输出-1。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
// String[] split = sc.nextLine().split(" ");
// int x = Integer.parseInt(split[0]);
// int n = Integer.parseInt(split[1]);
int x = Integer.parseInt(sc.next());
int n = Integer.parseInt(sc.next());
System.out.println(fun(x, n));
}
sc.close();
}
private static String fun(int x, int n) {
int[] a = new int[20];
for (int i = 0; i < 20; i++) {
a[i] = i + 1;
}
for (int i = 1; i <= n; i++) {
int findNum = (x + (i + 1) * i / 2 - 1) > 20 ? (x + (i + 1) * i / 2 - 1) % 20 : (x + (i + 1) * i / 2 - 1);
if (findNum != 0 && a[findNum - 1] != -1) {
a[findNum - 1] = -1;
} else if (findNum == 0) {
a[19] = -1;
}
}
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 20; i++) {
if (a[i] != -1) {
sb.append(a[i]).append(" ");
}
}
if (sb.length() == 0)
return "-1";
return sb.toString();
}
}