模运算符的用途

发布于 2024-09-28 04:20:51 字数 85 浏览 1 评论 0原文

模运算符有哪些用途?我知道它计算除法的余数,所以我真的想问余数有什么用处?

到目前为止,我已经用它来检查数字是否为偶数以及表格上的交替颜色。

What are some uses of the modulus operator? I know that it calculates the remainder in division so really I am asking what uses does the remainder have?

So far I have used it to check if a number was even and alternate colors on a table.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(10

作业与我同在 2024-10-05 04:20:51
for(int i=0;i<10;i++)
{
  if((i % 2) == 0 )
  {
   // I'm in an even row
  }else{
   // I'm in an odd row
  }
}  

最基本的使用

注意:lang使用Java

for(int i=0;i<10;i++)
{
  if((i % 2) == 0 )
  {
   // I'm in an even row
  }else{
   // I'm in an odd row
  }
}  

The most basic use

Note: lang used Java

余罪 2024-10-05 04:20:51

通过每隔多次迭代打印一条消息来获取长时间运行循环中的进度指示。

List<Thing> bigList = readBigList();

for (int i = 0; i < bigList.size(); i++) {
    processThing(bigList.get(i));
    if (i % 10000 == 0) {
        LOG.info("Processed " + i + " out of " + bigList.size() + " items");
    }
}

Getting an indication of progress in a long running loop by printing a message once every so many iterations.

List<Thing> bigList = readBigList();

for (int i = 0; i < bigList.size(); i++) {
    processThing(bigList.get(i));
    if (i % 10000 == 0) {
        LOG.info("Processed " + i + " out of " + bigList.size() + " items");
    }
}
荒岛晴空 2024-10-05 04:20:51
  • 单位转换,例如 13425 m13425 / 1000 km 和 13425 % 1000 m = 13 km 和 425 m
  • 随机数修剪,如果您'使用 C/C++ 的 rand() 时,常见的习惯用法是 rand() % (HIGH - LOW) + LOW 生成 HIGH 和 LOW
  • 模算术之间的随机数:角度限制为 360 度或2*pi,您可以使用模运算
  • 符偶数/奇数检查标准化它们的范围:如果“n % 2”为真,则 n 为偶数,否则为奇数
  • Unit conversion, e.g. 13425 m is 13425 / 1000 km and 13425 % 1000 m = 13 km and 425 m
  • random number trimming, if you're using C/C++'s rand(), a common idiom is rand() % (HIGH - LOW) + LOW to generate a random number between HIGH and LOW
  • modular arithmetic: angles are limited to 360 degrees or 2*pi, you can normalize their range using modulus operator
  • even/odd check: if "n % 2" is true then n is even otherwise it's odd
情域 2024-10-05 04:20:51
  • Primes
  • 将数字从 x 基数转换为 y 基数
  • Primes
  • Convert numbers from base x to base y
孤君无依 2024-10-05 04:20:51

72 分钟模 60 = 整点过 12 分钟

72 minutes modulo 60 = 12 minutes past the hour

烙印 2024-10-05 04:20:51

按位计算,包括条件检查。

Bitwise calculations, including conditional checking.

你的笑 2024-10-05 04:20:51

中国算术(这是首选的术语吗,老兄?)

Chinese arithmetic (is that the preferred nomenclature, dude?)

以为你会在 2024-10-05 04:20:51

模运算符是时钟算术中最重要的运算符。

The modulus operator is the single-most important operator in Clock Arithmetic.

山色无中 2024-10-05 04:20:51

它通常用于检查一个数字是否可以被另一个数字整除。

if(number % 2 == 0){
    // the number is even
} else {
    // the number is odd
}

if(number % 3 == 0){
    // the number is evenly divisible by three
} else {
    // the number is not evenly divisible by three
}

如果 mod 运算的结果为 0,则被除数(数字)可被除数整除。

您可以利用此功能对表数据执行“钢琴键”样式的交替行着色,或者每 X 行打印新的列标题,或者您拥有的其他操作。

It's generally used to check if one number is evenly divisible by another.

if(number % 2 == 0){
    // the number is even
} else {
    // the number is odd
}

or

if(number % 3 == 0){
    // the number is evenly divisible by three
} else {
    // the number is not evenly divisible by three
}

If the result of a mod operation is 0, the dividend (number) is evenly divisible by the divisor.

You can take advantage of this to do things like "piano-keys" style alternate-row shading on table data, or printing new column headings every X number of rows, or what have you.

不甘平庸 2024-10-05 04:20:51

编程 101 示例是调制数据的行颜色:

for(int i = 0; i < 100; i++)
{
    write-color i % 2;
}

确定数字是奇数还是奇数:

return number % 2;

A programming 101 exapmle would be to modulate row colors for data:

for(int i = 0; i < 100; i++)
{
    write-color i % 2;
}

Determine if a number is evan or odd:

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