提出链表的简单公式

发布于 2024-10-19 09:05:02 字数 579 浏览 0 评论 0原文

老实说,我已经为此好几天了。我已经实现了这个函数的困难部分,但现在只剩下一件小事了。我要写的方法是删除链表中blockSize的每第N块。因此,如果我有一个大小为 7 的链表 {1,2,3,4,5,6,7}, N=2, blockSize=2,我想删除每个第 N(2nd) 个大小为 blockSize(2) 的块,因此删除 3,4,7。现在,为了让 for 循环正常工作,我需要为我创建的名为 numBlocksRemoved 的 int 值编写一个表达式。它计算要删除的块的总数。在本例中,它将是 2。这就是我所拥有的:

numBlockRemoved=(size/blockSize)/N;

但是,这只在数字看起来不错的情况下有时有效。如果我有 size=8,N=2, blockSize=2,那么我会得到 numBlockRemoved=2,这是正确的。但是,对于上面的示例,我输入的 int 值是 1,这是不正确的。我想要 2。我已经想了很长时间了,这很荒谬。我只是想不出适用于 numBlockRemoved 的公式。有什么想法吗?

I've been at this for days honestly. I've already implememnted the hard part of this function, but now theres just one small thing. The method I want to write is to remove every Nth block of blockSize of a linked list. So if I have a linked list of size 7 {1,2,3,4,5,6,7}, N=2, blockSize=2, I want to remove every Nth(2nd) block of size blockSize(2), so remove 3,4,7. Now in order for my for loops to work, I need to write an expression for an int value I created called numBlocksRemoved. It calculates the total number of blocks to be removed. In this case, it would be 2. Here's what I have:

numBlockRemoved=(size/blockSize)/N;

However, this only works sometimes, when the numbers are looking good. If I have size=8,N=2, blockSize=2, then I get numBlockRemoved=2, which is correct. However, for the above example, I get in int value of 1, which is incorrect. I want 2. I've thought about this for soooo long its ridiculous. I just cant come up with a formula that works for numBlockRemoved. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

情深已缘浅 2024-10-26 09:05:02

尝试

floor(ceil(size/blockSize)/N)

floor(ceil(7/2)/3) = 1
floor(ceil(7/2)/2) = 2
floor(ceil(8/2)/2) = 2

您拥有的块数:

blocks = ceil(size/blockSize)

ceil,因为您不介意未满的块。

然后你跳过每一个 N,所以:

floor(blocks/N)

floor 因为你要么计算一个块,要么不计算。

Try

floor(ceil(size/blockSize)/N)

floor(ceil(7/2)/3) = 1
floor(ceil(7/2)/2) = 2
floor(ceil(8/2)/2) = 2

The number of blocks that you have:

blocks = ceil(size/blockSize)

ceil because you don't mind for not-full blocks.

then you skip every N, so:

floor(blocks/N)

floor because you either count a block or you don't.

我不吻晚风 2024-10-26 09:05:02

当计算块的数量时,应向上舍入,因为不完整的块仍然是一个块(但在计算删除的块的数量时则不是):

numBlockRemoved=((size+blockSize-1)/blockSize)/N;

Rounding should be upward when computing the number of blocks as an incomplete block is still a block (but not when computing the number of removed blocks):

numBlockRemoved=((size+blockSize-1)/blockSize)/N;
风和你 2024-10-26 09:05:02

(大小 + (块大小 - 1)) / (块大小 * N)

(size + (blockSize - 1)) / (blockSize * N)

梅窗月明清似水 2024-10-26 09:05:02

只要系统地考虑一下 - 如果你采用每第 N 个大小为 blockSize 的块,那么你就有效地删除了大小为 (N * blockSize) 的“超级块”。因此,对于第一个近似值,

nBlocks = floor [size / (N * blockSize)]

现在,从您的示例来看,即使您最后没有得到完整的块,您仍然想删除它。如果删除最后一个完整块后的余数多于 (N-1) 个完整块,就会发生这种情况。所以算法是

superblock = N * blockSize
nBlocks = floor (size / superblock)
remainder = size - (nBlocks * superblock)
if remainder > (N - 1) * blockSize {
  nBlocks += 1
}

你​​可以将最后的 +1 调整折叠到公式中,方法是添加一个量,该量将使整个超级块的大小倾斜,当且仅当它距离小于一个块时(类似于通过添加 0.5 进行四舍五入,然后取地面)。由于如果我们在超级块的最后一个块中只有一个数字,就会发生这种情况,因此我们必须添加 (blockSize - 1),这给我们提供了

(size + (blockSize - 1)) / (blockSize * N)

上面的 aaz 公式。因此,您可以继续将他的答案标记为已接受;我只是想解释一下他是如何得出这个公式的。

Just think about it systematically - if you take every Nth block of size blockSize, you are effectively removing "superblocks" of size (N * blockSize). So to a first approximation, you have

nBlocks = floor [size / (N * blockSize)]

Now, from your example, even if you don't get a complete block at the end, you still want to remove it. This happens if the remainder after removing the last complete block is more than (N-1) complete blocks. So the algorithm is

superblock = N * blockSize
nBlocks = floor (size / superblock)
remainder = size - (nBlocks * superblock)
if remainder > (N - 1) * blockSize {
  nBlocks += 1
}

You can collapse the +1 adjustment at the end into the formula by adding an amount that will tip the size over a complete superblock iff it is less than one block away (analogous to rounding by adding .5 and then taking the floor). Since this happens if we are even one number into the last block of the superblock, we have to add (blockSize - 1), which gives us

(size + (blockSize - 1)) / (blockSize * N)

Which is aaz's formula above. So you can go ahead and mark his answer as accepted; I just wanted to explain how he arrived at that formula.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文