识别何时使用模运算符 [英] Recognizing when to use the modulus operator

查看:67
本文介绍了识别何时使用模运算符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道模量(%)运算符会计算除法的余数.如何确定需要使用模运算符的情况?

I know the modulus (%) operator calculates the remainder of a division. How can I identify a situation where I would need to use the modulus operator?

我知道我可以使用模运算符来查看一个数字是偶数还是奇数以及质数或复合数,但是仅此而已.我不常考虑余数.我确定模数运算符是有用的,并且我想学习利用它.

I know I can use the modulus operator to see whether a number is even or odd and prime or composite, but that's about it. I don't often think in terms of remainders. I'm sure the modulus operator is useful, and I would like to learn to take advantage of it.

我只是在确定模数运算符在哪里适用时遇到问题.在各种编程情况下,我很难看到问题并意识到嘿!除法的其余部分将在这里工作!".

I just have problems identifying where the modulus operator is applicable. In various programming situations, it is difficult for me to see a problem and realize "Hey! The remainder of division would work here!".

推荐答案

想象一下您有一个经过的时间(以秒为单位),并且想要将其转换为小时,分钟和秒:

Imagine that you have an elapsed time in seconds and you want to convert this to hours, minutes, and seconds:

h = s / 3600;
m = (s / 60) % 60;
s = s % 60;

这篇关于识别何时使用模运算符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆