当前位置: 首页 > 知识库问答 >
问题:

求由多个子组合数组组成的数组的索引

籍利
2023-03-14

此请求不使用2D数组。考虑以下结构的1D数组,其中索引从零开始,长度从一开始:

节0是单个单元格的数组。

第1节

    null
    null
    null
    null
 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();
    }
}

共有1个答案

楚苏燕
2023-03-14
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);
    }
}
 类似资料:
  • 我正在进行一项编码挑战,获取一个由子数组组成的给定数组,在每个子数组中搜索最大的数字,最后返回一个只包含最大数字的新数组。我的思路是从每个子数组中创建变量,编写一个for循环来比较数组中的每个值,然后将最大值推送到一个新数组中。在编写了我的第一个for循环之后,我测试了我的代码,发现我得到了一个意想不到的结果,整个第一个子数组被压入了我的新数组。在我写下三个循环之前,我在寻找错误。谢谢你。编辑:这

  • 这看起来很容易,但却不知道该怎么做。当前数组数据是按日期和日期列出的,因此我需要将所有日期组合起来:天、月、6个月、1年。我需要将数组数据排列为下面的第二个数组。

  • 给定两个数组,其中数组1包含变量,数组2包含变量的值,我希望得到这两个数组的所有可能组合,例如: 数组可以容纳尽可能多的变量,也可以容纳。 所需输出: 我尝试了一种递归方法: 我希望能够将每一行正确的内容保存为中的一个元素,但首先我想知道如何正确打印每一行。 编辑:第二个数组不是常量,它在每个变量之间不同: 显然,这在递归中行不通,因为我需要将每个数组与变量数组一起发送,我通过创建一个对象来实现这

  • 问题内容: 我有一个索引元组数组,我想用它从多维numpy数组中选取值, 理解只有在已知的情况下才有效。 有什么提示吗? 问题答案: 您可以将的转置版本转换为元组,然后为矢量化解决方案建立索引-

  • 我试图像这样整合多个数组的数组 要接收此

  • 我有两个多维数组,我想通过使用其中一个数组的值和另一个数组的键来组合它们。数组如下: 阵列1: 阵列 2: 我想生成的结果数组如下: 有没有一个PHP函数可以用来完成这个任务?