简体   繁体   English

模运算符如何工作?

[英]How does the modulus operator work?

Let's say that I need to format the output of an array to display a fixed number of elements per line.假设我需要格式化数组的输出以显示每行固定数量的元素。 How do I go about doing that using modulus operation?我如何使用模数运算来做到这一点?

Using C++, the code below works for displaying 6 elements per line but I have no idea how and why it works?使用 C++,下面的代码可用于每行显示 6 个元素,但我不知道它是如何以及为什么工作的?

for ( count = 0 ; count < size ; count++)
{
    cout << somearray[count];
    if( count % 6 == 5) cout << endl;
}

What if I want to display 5 elements per line?如果我想每行显示 5 个元素怎么办? How do i find the exact expression needed?我如何找到所需的确切表达?

in C++ expression a % b returns remainder of division of a by b (if they are positive. For negative numbers sign of result is implementation defined).在 C++ 表达式中, a % b返回 a 除以 b 的余数(如果它们是正数。对于结果的负数符号是实现定义的)。 For example:例如:

5 % 2 = 1
13 % 5 = 3

With this knowledge we can try to understand your code.有了这些知识,我们可以尝试理解您的代码。 Condition count % 6 == 5 means that newline will be written when remainder of division count by 6 is five.条件count % 6 == 5表示当除法计数的余数为 5 时将写入换行符。 How often does that happen?这种情况多久发生一次? Exactly 6 lines apart (excercise : write numbers 1..30 and underline the ones that satisfy this condition), starting at 6-th line (count = 5).正好相隔 6 行(练习:写数字 1..30 并在满足此条件的数字下划线),从第 6 行(计数 = 5)开始。

To get desired behaviour from your code, you should change condition to count % 5 == 4 , what will give you newline every 5 lines, starting at 5-th line (count = 4).要从您的代码中获得所需的行为,您应该将条件更改为count % 5 == 4 ,这将使您每 5 行换行一次,从第 5 行(count = 4)开始。

Basically modulus Operator gives you remainder simple Example in maths what's left over/remainder of 11 divided by 3?基本上模数运算符给你余数简单的数学示例 11 除以 3 的余数/余数是多少? answer is 2答案是 2

for same thing C++ has modulus operator ('%')对于同样的事情 C++ 有模运算符 ('%')

Basic code for explanation解释的基本代码

#include <iostream>
using namespace std;


int main()
{
    int num = 11;
    cout << "remainder is " << (num % 3) << endl;

    return 0;
}

Which will display哪个会显示

remainder is 2余数为 2

This JSFiddle project could help you to understand how modulus work: http://jsfiddle.net/elazar170/7hhnagrj这个 JSFiddle 项目可以帮助您了解模数是如何工作的: http : //jsfiddle.net/elazar170/7hhnagrj

The modulus function works something like this:模数函数的工作原理是这样的:

     function modulus(x,y){
       var m = Math.floor(x / y);
       var r = m * y;
       return x - r;
     }

It gives you the remainder of a division.它给你一个除法的剩余部分。

int c=11, d=5;
cout << (c/d) * d + c % d; // gives you the value of c

You can think of the modulus operator as giving you a remainder.您可以将模运算符视为给您一个余数。 count % 6 divides 6 out of count as many times as it can and gives you a remainder from 0 to 5 (These are all the possible remainders because you already divided out 6 as many times as you can). count % 6 将 6 尽可能多地除以计数,并为您提供从 0 到 5 的余数(这些是所有可能的余数,因为您已经尽可能多地除以 6)。 The elements of the array are all printed in the for loop, but every time the remainder is 5 (every 6th element), it outputs a newline character.数组的元素都在for循环中打印出来,但是每次余数为5(每6个元素)时,它输出一个换行符。 This gives you 6 elements per line.这为您提供每行 6 个元素。 For 5 elements per line, use对于每行 5 个元素,请使用

if (count % 5 == 4)如果(计数 % 5 == 4)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM