需要平等地增加价值

如何解决需要平等地增加价值

我必须遵循以下数组:

var collections=[
    {value:20,next:"",prev: "",lastUpdated: false,isLoaked: false,pointer: false},{value:20,pointer: false}
];

使用以下迭代,我可以计算nextprev

for(i = 0; i < collections.length; i++) {
    collections[i].next = (i+1) % collections.length;
  collections[i].prev = i > 0 ? i-1 : collections.length-1;
}

以下是结果:

var collections=[
    {value:20,next:"1",prev: "3",next:"2",prev: "0",next:"3",prev: "1",next:"4",prev: "2",next:"0",pointer: false}
];

基于新集合,如果指针是increaseddecreased

,我需要计算每个值

示例:第三项的值已增加到25

var collections=[
    {value:20,{value:25,pointer: true},pointer: false}
];

预期结果:

0: {value: 19,next: 1,prev: 4,lastUpdated: true,pointer: false}
1: {value: 19,next: 2,prev: 0,pointer: false}
2: {value: 25,next: 3,prev: 1,pointer: true}
3: {value: 18,next: 4,prev: 2,pointer: false}
4: {value: 19,next: 0,prev: 3,pointer: false}

示例2:如果同一指针的值减小为2。

var collections=[
    {value:19,{value:19,{value:23,{value:18,pointer: false}
];

预期以下结果,并遵循prev

0: {value: 20,pointer: false}
1: {value: 20,pointer: false}
2: {value: 23,pointer: false}

到目前为止,我有以下代码

var collections=[
    {value:20,pointer: false}
];
var maxValue = 100;
var totalValue = 0;
var toBedistributedNext = 0;
var toBedistributedPrev = 0;

for(i = 0; i < collections.length; i++) {
    collections[i].next = (i+1) % collections.length;
  collections[i].prev = i > 0 ? i-1 : collections.length-1;
}

collections[2].value = 25;
collections[2].pointer = true;

collections.forEach(function (collection) {
  totalValue += parseInt(collection.value);
});

if (totalValue > maxValue) {
    toBedistributedNext = totalValue - maxValue;
} else {
    toBedistributedPrev = maxValue - totalValue;
}
var currentPointer = collections.find(item => item.pointer === true);
for (let i = 0; i < toBedistributedNext; i++) {
  var nextIndex = currentPointer.next;
  if (!collections[i%collections.length].pointer) {
    collections[i%collections.length].value--;
    collections[i%collections.length].lastUpdated = true;
    console.log(collections[i%collections.length])
    //currentPointer = collections[i%collections.length].next;
  }
}

上面的代码给出以下结果:错误

0: {value: 19,…}
1: {value: 19,…}
2: {value: 25,…}
3: {value: 19,…}
4: {value: 19,…}

最后一个没有计算,请任何人帮助。 总数必须为100

我有Jsfiddlehttps://jsfiddle.net/tanvir_alam_shawn/zm21pLen/144/

解决方法

您非常接近,但是您的代码中有一些奇怪的事情。

首先,您为当前指针仔细分配:

var currentPointer = collections.find(item => item.pointer === true);

然后在循环中分配下一个指针

for (let i = 0; i < toBeDistributedNext; i++) {
    var nextIndex = currentPointer.next;

但是在下一行中,您忽略了所有好的工作:

if (!collections[i%collections.length].pointer)

collections[i%collections.length]并不指向您当前的指针,请注意在循环开始时i等于0。应该使用{{1}来代替用i访问指针。 }您已经知道的价值。

此外,您通过示例提高了样本集数组中的五个项的值5的范围,从而对自己毫无帮助。无论如何,看着你的循环,看看问题所在:

nextIndex

for (let i = 0; i < toBeDistributedNext; i++) { var nextIndex = currentPointer.next; if (!collections[i%collections.length].pointer) { collections[i%collections.length].value--; collections[i%collections.length].lastUpdated = true; console.log(collections[i%collections.length]) //currentPointer = collections[i%collections.length].next; } } 设置为true怎么办?好吧,值没有改变,但是无论如何我都会增加,因此实际上只进行了4次更改。因此,要修复您的代码,您想使用已经计算出的实际索引,并在当前收集项的指针设置为true时执行某些操作(例如,增量pointer)。您还希望更新currenntPointer的值,以便正确计算下一个索引。所以循环看起来像这样:

toBeDistributedNext

以下是代码正常工作的代码段:

var currentPointer = collections.find(item => item.pointer === true);
for (let i = 0; i < toBeDistributedNext; i++) {
  var nextIndex = currentPointer.next;
  if (!collections[nextIndex].pointer) {
    collections[nextIndex].value--;
    collections[nextIndex].lastUpdated = true;
  } else {
    toBeDistributedNext++;
  }

  currentPointer = collections[nextIndex];
}

,

您可以使用一个函数来更新值并检查增量分布。

function update(array,index,value) {
    function go(index,direction) {
        do {
            if (!array[index].isLocked) { // check for locked items
                array[index].value += increment;
                if (!(delta -= increment)) return;
            }
            index = array[index][direction];
        } while (!array[index].pointer)
    }
    
    let delta = array[index].value - value,increment = Math.sign(delta);

    array[index].value = value;
    array[index].pointer = true;

    while (delta) {
        go(array[index].prev,'prev');
        if (!delta) break;
        go(array[index].next,'next');
    }

    array[index].pointer = false;
}

var collections = [
        { value: 20,next: 1,prev: 4,lastUpdated: false,isLocked: false,pointer: false },{ value: 20,next: 2,prev: 0,next: 3,prev: 1,next: 4,prev: 2,next: 0,prev: 3,pointer: false }
    ];

update(collections,2,25);
console.log(...collections.map(({ value }) => value));
update(collections,23);
console.log(...collections.map(({ value }) => value));

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?