查找一个不接近 NSMutableArray 中任何整数的整数

发布于 2024-12-09 11:08:21 字数 549 浏览 0 评论 0原文

我有一个 NSMutableArray,它有 5 个值最初设置为零。它包含屏幕上元素的 x 坐标,每 1/60 秒更新一次。每个项目的位置都在不断变化。屏幕上最多有 5 个项目,任何时候最少有 1 个。

数组中的每个项目的范围为 0 - 480(iPhone 屏幕的高度)。给定时间的示例数组为:

{123,450,0,0,0}

然后可能会更改为:

{150,320,90,0,0}

我需要一种快速查找不靠近数组中任何位置的位置的方法。这可以是不在每个值周围的设定范围内的位置(例如,该值不在数组中任何项目的 50 个范围内),也可以是其两侧距离最远的位置。

显然,如果不可能在一定范围内找到位置,则应该选择最佳解决方案。

当新项目添加到屏幕上时,它需要很快,因此选择新位置的任何延迟都会减慢游戏的速度 - 因此 while 循环并不可取。

希望 Objective-C 中有一个简单的数学方法可以解决这个问题。我真的很困惑应该如何实现这一目标。任何帮助都非常感谢。

I have a NSMutableArray that has 5 values initially set to zero. It contains the x-coordinate of elements on the screen and is updated every 1/60th of a second. The position of each item is constantly changing. There is a maximum of 5 items on the screen and a minimum of 1 at any time.

Each item in the array will be in the range 0 - 480 (height of iphone screen). An example array at a given time would be:

{123,450,0,0,0}

Which might then change to:

{150,320,90,0,0}

I need a quick way to find a position that is not near any position in the array. This can be a position that is not within a set range around each value (for example the value is not within 50 of any items in the array) or the position with the most distance either side of it.

Obviously if finding a position within a certain range is not possible, the best solution should be picked.

It needs to be quick as a new item is being added to the screen so any delay in picking a new position will slow down the game ticker - so a while loop is not preferable.

Hopefully theres a simple mathematical method in objective-c that can sort this. I really am stuck as to how I should achieve this. Any help is MUCH appreciated.

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

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

发布评论

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

评论(2

甩你一脸翔 2024-12-16 11:08:21

让我稍微改一下这个问题。你有点p1,p2,p3,p4,p5

0 <= p_i <= 480

你想找到一个点x,使得

0 <= x <= 480

函数最大化

min_{x} ( |p1-x| + |p2-x| + |p3-x| + |p4-x| + |p5-x| )

如果这是你的目标(而且它不是我不清楚它是或应该是),那么您可以通过检查以下哪个潜在 x 值最大化距离来解决此问题:

0 , p1/2 , p1 + (p2-p1)/2 , p2 + (p3-p2)/2 , ... , p5 + (480-p5)/2

假设 0 <= p1 < ;= p2 <= p3 <= p4 <= p5 <= 480。您应该选择差异项中较大的答案。

例如,

{123,450,0,0,0}

答案是123 + (450-123)/2

{150,320,90,0,0} 

答案是 150 + (320-150)/2

在 Objective-C 中进行编码,您需要一个函数来返回最大条目的索引一个数组。将输入设为p1,p2,...,p5。将它们按升序排列,在左侧附加 0,在右侧附加 480。然后创建一个长度减一的新数组,给出连续的差异,例如 {p1-p0, p2-p1, ..., p6-p5} 其中 p0 = 0p6 = 480。最后,获取这个新数组的最大值的索引,将其称为i,并返回最佳位置p_i + (p(i+1)-p_i)/2

示例:

输入{150,320,90,0,0}

重新排列为{0 , 0 , 0 , 90 , 150 , 320 , 480}

索引处的差异数组 {0 , 0 , 90 , 60 , 170 , 160}

最大值4

答案是 150 + (320-150)/2

Let me rephrase this question a bit. You have points p1,p2,p3,p4,p5 such that

0 <= p_i <= 480

You want to find a point x such that

0 <= x <= 480

that maximizes the function

min_{x} ( |p1-x| + |p2-x| + |p3-x| + |p4-x| + |p5-x| )

If this is your aim (and it isn't clear to me that it is or should be), then you can solve this problem by checking which of the following potential x values maximizes the distance:

0 , p1/2 , p1 + (p2-p1)/2 , p2 + (p3-p2)/2 , ... , p5 + (480-p5)/2

this assumes 0 <= p1 <= p2 <= p3 <= p4 <= p5 <= 480. Whichever of the difference terms is larger is the answer you should select.

For example, for

{123,450,0,0,0}

the answer is 123 + (450-123)/2.

For

{150,320,90,0,0} 

the answer is 150 + (320-150)/2

To code this in Objective-C, you need to have a function that returns the index of the maximum entry of an array. Take the input to be the p1,p2,...,p5. Sort these into increasing order, append 0 on the left and 480 on the right. Then make a new array of length one less that gives successive differences, e.g. {p1-p0, p2-p1, ..., p6-p5} where p0 = 0 and p6 = 480. Finally, get the index of the maximum of this new array, call it i, and return the optimal position p_i + (p(i+1)-p_i)/2.

Example:

input: {150,320,90,0,0}

rearrange to {0 , 0 , 0 , 90 , 150 , 320 , 480}

difference array {0 , 0 , 90 , 60 , 170 , 160}

maximum at index 4

answer is 150 + (320-150)/2

放飞的风筝 2024-12-16 11:08:21

我不认为有一个 Objective-C 对象可以为你做这件事——你实际上必须做一些编程。

我想到的第一个想法是某种树,但简单的 B 树或其他什么都不是。也许是一棵笛卡尔树,但我现在还不知道如何应用它。

或者你可以直接暴力破解——将值按顺序排列并扫描列表以找到最大的差距。

I don't think there's an Objective-C object that will do this for you -- you're actually going to have to do some programming.

The first idea that comes to mind is some sort of a tree, but a simple B-tree or what-have-you isn't it. Perhaps a Cartesian tree, but exactly how to apply that doesn't come to me right now.

Or you could just brute-force it -- place the values in order and scan the list to find the biggest gap.

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