【问题标题】:How to make a random super increasing array如何制作随机超递增数组
【发布时间】:2021-07-07 16:38:36
【问题描述】:

我有一个关于制作 Merkle Hellman 背包的实验室,请求说我需要制作一个自动超级增加的数组,但我不知道如何制作,有什么方法可以制作吗?感谢阅读

Random random = new Random();
int[] wInt = random.ints(8, 1, 999).toArray();
for(int i = 0; i < 8 - 1; i++) {
    for (int j = i + 1; j < wInt.length; j++) {
        if(wInt[i] > wInt[j]) {
            int temp = wInt[i];
            wInt[i] = wInt[j];
            wInt[j] = temp;
        }
    }
}

【问题讨论】:

  • random.ints(...).sorted().toArray()?不确定“自动超递增数组”是什么,但看起来您正在尝试对其进行排序。

标签: java arrays random knapsack-problem


【解决方案1】:

superincreasing sequence 是其中每个项至少与前面所有项的总和一样大的项。因此,生成超增序列的一种方法是跟踪当前序列中所有元素的总和,然后在形成下一个元素时将一些随机的非零数字添加到总数中。在伪代码中:

sequence = []
total = 0
while the sequence has fewer than n items in it:
    total += some random number
    append total to the sequence

我将把它作为练习翻译成你选择的编程语言。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 1970-01-01
    • 2016-10-24
    • 2013-12-25
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    相关资源
    最近更新 更多