这个请求不使用二维数组。考虑以下结构的一维数组,其中索引从零开始,长度从一开始:
第 0 部分是单个单元格的数组。
第 1 节
0
1
n - 1
0
1
n - 1
0
1
m - 1
第 2 节和第 3 节的结构与第 1 节相同。
有多个阵列拼凑在一起以模拟更大的阵列。总共有四个部分:部分 0 将始终具有单个单元格,部分 1、2 和 3 将每个具有 k
子数组。子数组 1
到 k-1
(最后一个子数组除外)的长度为 n
。最后一个子数组将包含 m
元素和 m < n
。
目标:给定一个从零开始的累积索引input
,使用索引公式找到一个section的子数组中对应的索引,最少的if
语句和没有循环。
这是一个例子:
k = 3
、n = 8
和 m = 5
都从一开始。以下是第 0 节和第 1 节的正确输出: input = 0 → section = 0, array = 0, subIndex = 0
input = 1 → section = 1, array = 0, subIndex = 0
input = 2 → section = 1, array = 0, subIndex = 1
input = 3 → section = 1, array = 0, subIndex = 2
input = 4 → section = 1, array = 0, subIndex = 3
input = 5 → section = 1, array = 0, subIndex = 4
input = 6 → section = 1, array = 0, subIndex = 5
input = 7 → section = 1, array = 0, subIndex = 6
input = 8 → section = 1, array = 0, subIndex = 7
input = 9 → section = 1, array = 1, subIndex = 0
input = 10 → section = 1, array = 1, subIndex = 1
input = 11 → section = 1, array = 1, subIndex = 2
input = 12 → section = 1, array = 1, subIndex = 3
input = 13 → section = 1, array = 1, subIndex = 4
input = 14 → section = 1, array = 1, subIndex = 5
input = 15 → section = 1, array = 1, subIndex = 6
input = 16 → section = 1, array = 1, subIndex = 7
input = 17 → section = 1, array = 2, subIndex = 0
input = 18 → section = 1, array = 2, subIndex = 1
input = 19 → section = 1, array = 2, subIndex = 2
input = 20 → section = 1, array = 2, subIndex = 3
input = 21 → section = 1, array = 2, subIndex = 4
接下来的条目应该是:
input = 22 → section = 2, array = 0, subIndex = 0
...
input = 30 → section = 2, array = 1, subIndex = 0
...
input = 38 → section = 2, array = 2, subIndex = 0
...
input = 42 → section = 2, array = 2, subIndex = 4
input = 43 → section = 3, array = 0, subIndex = 0
...
input = 51 → section = 3, array = 1, subIndex = 0
...
input = 59 → section = 3, array = 2, subIndex = 0
...
input = 63 → section = 3, array = 2, subIndex = 4
截至目前,我的带有单个 if
语句的公式只能获取第一部分(不是第 2 部分或第 3 部分)。希望找到一组公式来实现目标,而无需使用 if
语句或循环。这是我目前所拥有的:
public class Test {
public Test() {
for (int i = 0; i < 63; i++) {
getIndex(i);
}
}
public void getIndex(int i) {
if (i == 0) {//Aligns output. I couldn't factor it in the formula
System.out.println("input = " + i + " → " +
"section = " + 0 +
", array = " + 0 +
", subIndex = " + 0);
return;
}
int k = 3;//Number of subarrays for each section
int n = 8;//all subarray sizes except last one
int m = 5;//last subarray size
int sectionLength = (k - 1) * n + m;//number of cells in a single
//section (other than section 0).
//obtaining the values:
int section = (i - 1) / sectionLength + 1;//the section (1, 2 or 3)
int subarray = (i - 1) / (section * n);
int index = ((i - 1) % n);
System.out.println("input = " + i + " → " +
"section = " + section +
", array = " + subarray +
", subIndex = " + index);
}
public static void main(String[] args) {
Test t = new Test();
}
}
答案 0 :(得分:0)
public class Test {
public static void main(String[] args) {
// all your constants
final int TOTAL_SECTION = 63;
final int SUBARRAY_PER_SECTION = 3;
final int SUBARRAY_SIZE = 8;
final int FINAL_SUBARRAY_SIZE = 5;
// keep track of current input number, increase one after each input
int inputNumber = 0;
for (int i = 0; i < TOTAL_SECTION; i++) {
// if it is 0, all the value is 0, so just print the defualt format with all 0
if (i == 0) {
print(inputNumber++, 0 , 0, 0);
System.out.println();
continue;
}
for (int j = 0; j < SUBARRAY_PER_SECTION; j++) {
// if it is last subarray, use the final subarray size
if (j == SUBARRAY_PER_SECTION - 1) {
for (int k = 0; k < FINAL_SUBARRAY_SIZE; k++) {
print(inputNumber++, i, j, k);
}
break;
}
// otherwise, just use the normal subarray size
for (int k = 0; k < SUBARRAY_SIZE; k++) {
print(inputNumber++, i, j, k);
}
}
// print a new line after each section done
System.out.println();
}
}
// format of the message, use this method and give the 4 values needed as parameters
private static void print(int inputNumber, int sectionNumber, int arrayNumber, int subIndex) {
System.out.printf("input = %2d → section = %d, array = %d, subIndex = %d %n", inputNumber, sectionNumber, arrayNumber, subIndex);
}
}