此处的问题链接https://www.urionlinejudge.com.br/judge/en/problems/view/1030
我正在尝试提交此问题的答案,但我得到的只是运行时错误。它说这是因为数组索引(或类似的)错误,但是我认为情况并非如此,因为我使用的是ArrayLists,并且在我的计算机上输出正确。
import java.io.IOException;
import java.util.Scanner;
import java.util.ArrayList;
public class Main {
static class Pair {
public int size;
public int step;
public Pair(int l, int r) {
size = l;
step = r;
}
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
final int tests = s.nextInt();
int[] res = new int[tests];
Pair[] inputs = new Pair[tests];
// take input first
for (int i = 0; i < tests; i++)
inputs[i] = new Pair(s.nextInt(), s.nextInt());
for (int i = 0; i < tests; i++) {
final int step = inputs[i].step;
int size = inputs[i].size;
int pos = step;
ArrayList<Integer> nums = new ArrayList<Integer>();
for (int c = 0; c < inputs[i].size; c++)
nums.add(new Integer(c));
if (size == 1) {
res[i] = nums.get(0);
continue;
}
while (size > 1) {
nums.remove(pos);
size--;
pos--;
pos = pos + step;
if (pos >= size)
pos = pos % size;
}
res[i] = nums.get(0);
}
int count = 1;
for (int i : res) {
System.out.println("Case " + count++ + ": " + i);
}
}
}
我在IntelliJ IDEA上获得的输出与示例中的输出一样,因此,我相信此解决方案可以做到。