C++ 模运算符如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12556946/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does the modulus operator work?
提问by Tex Qas
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? How do i find the exact expression needed?
如果我想每行显示 5 个元素怎么办?我如何找到所需的确切表达?
回答by KCH
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). For example:
在 C++ 表达式中a % b
返回 a 除以 b 的余数(如果它们是正数。对于结果的负数符号是实现定义的)。例如:
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. 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).
有了这些知识,我们可以尝试理解您的代码。条件count % 6 == 5
意味着当除法计数的余数为 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 行(计数 = 4)开始。
回答by Atri Dave
Basically modulus Operator gives you remainder simple Example in maths what's left over/remainder of 11 divided by 3? answer is 2
基本上模数运算符给你余数简单的数学示例 11 除以 3 的余数/余数是多少?答案是 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
回答by Gung Foo
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
回答by Andy Harris
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). 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. This gives you 6 elements per line. For 5 elements per line, use
您可以将模运算符视为给您一个余数。count % 6 将 6 尽可能多地除以计数,并为您提供从 0 到 5 的余数(这些是所有可能的余数,因为您已经尽可能多地除以 6)。数组的元素都在for循环中打印出来,但是每次余数为5(每6个元素)时,它输出一个换行符。这为您提供每行 6 个元素。对于每行 5 个元素,请使用
if (count % 5 == 4)
如果(计数 % 5 == 4)
回答by SkySibe
This JSFiddle project can helps you to understand how modulus work: http://jsfiddle.net/elazar170/7hhnagrj
这个 JSFiddle 项目可以帮助你理解模数是如何工作的:http: //jsfiddle.net/elazar170/7hhnagrj
function modulus(x,y){
var m = Math.floor(x / y);
var r = m * y;
return x - r;
}