帮助尝试理解圆形数组中的模运算

发布于 2024-11-06 10:43:30 字数 763 浏览 3 评论 0原文

我有一个小问题试图弄清楚模运算是如何计算的。 我正在建立一个队列,所以我有一个圆形数组。 我无法弄清楚这个模运算是如何工作的。

给定 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 技术交流群。

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

发布评论

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

评论(2

断桥再见 2024-11-13 10:43:30

我认为您对模运算的作用有点困惑。它给出除法后的整数余数。所以从你的例子来看。

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).

鲜肉鲜肉永远不皱 2024-11-13 10:43:30

整数运算只会产生整数。虽然模数与除法相关,但它不是除法。

a % b is the same as (a - a / b * b) 

作为循环它是一样的。 (假设 b 为正)

int result = a;
while(a >= b) a -= b;
while(a + b <= 0) a += b;

但是对于 rare = (rare + 1) % MAX 来说,它是相同的。

rare = (rare == MAX - 1 ? 0 : rare + 1);

integer arithmetic will only result in integers. While modulo is related to division it is not division.

a % b is the same as (a - a / b * b) 

As loops it is the same as. (Assuming b is positive)

int result = a;
while(a >= b) a -= b;
while(a + b <= 0) a += b;

However for rare = (rare + 1) % MAX, it is the same as.

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