帮助尝试理解圆形数组中的模运算
我有一个小问题试图弄清楚模运算是如何计算的。 我正在建立一个队列,所以我有一个圆形数组。 我无法弄清楚这个模运算是如何工作的。
给定 q:一个 5 个元素长度的字符数组, MAX 常量给出数组的最大长度“5” rare 是一个 int,表示数组 q 中的第一个可用位置
public void enqueue(Character c)throws FullQueueException{
if(size()== MAX -1){ //if only 1 place left, is full, throw exc
throw new FullQueueException("Queue is full");
}
q[rare]=c;
rare=(rare+1)%MAX;
}
现在,假设罕见的“第一个空位置”是 3,那么该方法完成后,罕见的值将是多少? 这是我不明白的,稀有=(稀有+1)%MAX意味着稀有= 4%5,这给出了稀有= 0,8。
方法大小相同:
public int size() {
return (MAX - front + rear) % MAX;
}
前面给定一个 int 变量,它表示数组中的第一个元素 假设 front 为 1,rare 为 4,因此数组中有 3 个元素,因此大小为 (5-1+4)%5,即 8%5,即 1.6,而实际大小为 3 有什么建议吗?这可能比java更数学,但可能你们中的一些人之前遇到过同样的疑问。 谢谢你!
i have a small issue trying to figure out how a modulo operation is being calculated.
I am building up a queue, so i have a circular array.
i cannot figure out how this modulo operation works.
Given q: an array of Character of 5 elements length,
The MAX constant gives the max length of the array "5"
rare is an int which represents the first available spot in the array q
public void enqueue(Character c)throws FullQueueException{
if(size()== MAX -1){ //if only 1 place left, is full, throw exc
throw new FullQueueException("Queue is full");
}
q[rare]=c;
rare=(rare+1)%MAX;
}
Now, supposing that the rare "first empty spot" is three, what is the rare value going to be after the method has finished?
this is what i dont get, rare=(rare+1)%MAX means rare=4%5 which gives rare=0,8.
Same for method size:
public int size() {
return (MAX - front + rear) % MAX;
}
Given, front, an int variable which represents the first element in the array
Suppose front is 1 and rare 4, so there are 3 elements in the array, so size is (5-1+4)%5 which is 8%5 which gives 1.6, while the actual size is 3
Any suggestion? this might be more math then java but probably some of you came across the same doubt before.
Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您对模运算的作用有点困惑。它给出除法后的整数余数。所以从你的例子来看。
4 % 5 = 4 (因为 4/5 是 0,余数为 4)
AND
8 % 5 = 3 (因为 8/5 是 1,余数为 3)
如果没有看到其余的实现,它有点困难进一步解释为什么使用模,但看起来它基本上是用来确保你的圆形数组环绕。即,当您到达数组末尾时(例如最大大小为 8 的数组的索引 7,您想要的下一个值将是第一个元素,即 8%8 或 0)。
I think you're a bit conofused as to what the modulo operation does. It gives the integer remainder after a division. So from your example.
4 % 5 = 4 (because 4/5 is 0, with a remainder of 4)
AND
8 % 5 = 3 (because 8/5 is 1 with a remainder of 3)
Without seeing the rest of your implementation, its a bit difficult to explain further why modulo is being used, but it looks like its basically being used to ensure that your circular array wraps around. i.e. when you hit the end of the array (say index 7, of an array with MAX size 8, the next value you would want would be the first element, which would be 8%8 or 0).
整数运算只会产生整数。虽然模数与除法相关,但它不是除法。
作为循环它是一样的。 (假设 b 为正)
但是对于
rare = (rare + 1) % MAX
来说,它是相同的。integer arithmetic will only result in integers. While modulo is related to division it is not division.
As loops it is the same as. (Assuming b is positive)
However for
rare = (rare + 1) % MAX
, it is the same as.