C++ 百分号是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1504420/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 20:12:49  来源:igfitidea点击:

C++ What does the percentage sign mean?

c++

提问by Jerry Coffin

I got this c++ macro and wonder what they mean by code%2 (the percentage sign) ?

我得到了这个 C++ 宏,想知道它们的代码%2(百分比符号)是什么意思?

#define SHUFFLE_STATEMENT_2(code, A, B)
switch (code%2)
{
  case 0 : A; B; break;
  case 1 : B; A; break;
}

回答by James Black

It is for taking a modulus.

它用于取模数。

Basically, it is an integer representation of the remainder.

基本上,它是余数的整数表示。

So, if you divide by 2 you will have either 0 or 1 as a remainder.

因此,如果您除以 2,您将得到 0 或 1 作为余数。

This is a nice way to loop through numbers and if you want the even rows to be one color and the odd rows to be another, modulus 2 works well for an arbitrary number of rows.

这是循环数字的好方法,如果您希望偶数行是一种颜色而奇数行是另一种颜色,则模数 2 适用于任意数量的行。

回答by Jerry Coffin

In case somebody happens to care: % really returns the remainder, notthe modulus. As long as the numbers are positive, there's no difference.

如果有人碰巧关心: % 真的返回余数,而不是模数。只要数字是正数,就没有区别。

For negative numbers there can be a difference though. For example, -3/2 can give two possible answers: -1 with a remainder of -1, or -2 with a remainder of 1. At least as it's normally used in modular arithmetic, the modulus is always positive, so the first result does not correspond to a modulus.

但是对于负数,可能会有所不同。例如,-3/2 可以给出两个可能的答案:-1 余数为 -1,或 -2 余数为 1。至少在模算术中通常使用它时,模数始终为正,因此第一个结果不对应于模数。

C89/90 and C++98/03 allow either answer though, as long as / and % produce answers that work together so you can reproduce the input (i.e. -1x2+-1->-3, -2x2+1=-3).

C89/90 和 C++98/03 允许任何一个答案,只要 / 和 % 产生一起工作的答案,这样你就可以重现输入(即 -1x2+-1->-3,-2x2+1=-3 )。

For newer versions of the standards (C99, C11 and C++11) there's no longer any choice: integer division must round toward 0. For example -3/2 mustgive -1 with a remainder of -1. -3/2 giving -2 with a remainder of 1 is no longer allowed.

对于较新版本的标准(C99、C11 和 C++11),不再有任何选择:整数除法必须向 0 舍入。例如 -3/2必须给出 -1,余数为 -1。-3/2 给出 -2 且余数为 1 不再被允许。

回答by Mark Ransom

It means the remainder of a division. In your case, divide by 2 and the remainder will be either 0 or 1.

这意味着除法的剩余部分。在您的情况下,除以 2,余数将为 0 或 1。

回答by mouviciel

It means modulo. Usually (x % 2)discriminates odd and even numbers.

这意味着模数。通常(x % 2)区分奇数和偶数。

回答by RED SOFT ADAIR

Thats the modulo. It returns whats left after division:

这就是模数。它返回除法后剩下的东西:

10/3 will give 3. - 1 is left.

10/3 将给出 3。 - 还剩 1 个。

10%3 gives this 1.

10%3 给出了这个 1。

回答by JonH

Modulo returns the remainder that is left after division. It is helpful when you're tasked with determining even / odd / prime numbers as an example:

取模返回除法后剩下的余数。例如,当您的任务是确定偶数/奇数/质数时,它会很有帮助:

Here's an example of using it to find prime numbers:

这是使用它查找素数的示例:

int main(void)

{ int isPrime=1; int n;

{ int isPrime=1; 国际n;

cout << "Enter n: ";
cin >> n;

for (int i=1; i<=n; i++)
{
    for (int j=2; j <= sqrt(static_cast<double>(i)); j++)
    {
        if(!(i%j))
        {
            isPrime=0;
            break;
        }

    }

    if (isPrime)
        cout << i << " is prime" << endl;
    isPrime=1;
}
return 0;

}

}

回答by Ryan Cori

It's the remainder of division. So like how 5 divided by 2 has a remainder of 1 because 2 goes into 5 2 times but that only equals four, and you got that little extra on the end

这是除法的剩余部分。所以就像 5 除以 2 的余数是 1 因为 2 进入 5 2 次但只等于 4,最后你得到了一点额外的

5 % 2 == 1

5 % 2 == 1

Note that division value isn't being calculated anywhere, so if you wanted both the whole total integer value of the division and it's remainder

请注意,除法值不会在任何地方计算,因此如果您想要除法的整个总整数值及其余数

int answer = 5 / 2;
int remainder = 5 % 2;
cout << "5 divided by 2 is " << answer << " with remainder " << remainder;

"5 divided by 2 is 2 with remainder 1"

“5 除以 2 是 2,余数是 1”