当第一个数小于第二个数时,模数除法 [英] Modulus division when first number is smaller than second number

查看:189
本文介绍了当第一个数小于第二个数时,模数除法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个简单的问题,我很抱歉,但是当第一个数字小于第二个数字时,我无法理解模数除法的概念。例如,当我的书中1%4表示余数为1.我不明白1是1%4的剩余部分。
1/4是0.25。我在考虑模数除法错误吗?

I apologize if this is a simple question but I'm having trouble grasping the concept of modulus division when the first number is smaller than the second number. For example when 1 % 4 my book says the remainder is 1. I don't understand how 1 is the remainder of 1 % 4.
1 / 4 is 0.25. Am I thinking about modulus division incorrectly?

推荐答案

首先,在Java中,%是余数(非模数)运算符,它的语义略有不同。
也就是说,你需要考虑整数除法,就好像没有小数值一样。可以将其视为存储无法分割的项目:您可以将大小为4的零项目存储在总容量为1的存储中。存储最大数量的项目后,您的剩余容量为1。同样,13%5是3,因为你可以在13个存储空间中容纳2个完整的5号物品,剩余容量 13 - 2 * 5 = 3

First, in Java, % is the remainder (not modulo) operator, which has slightly different semantics. That said, you need to think in terms of integer-only division, as if there were no fractional values. Think of it as storing items that cannot be divided: you can store zero items of size 4 in a storage of overall capacity one. Your remaining capacity after storing the maximum number of items is one. Similarly, 13%5 is 3, as you can fit 2 complete items of size 5 in a storage of size 13, and the remaining capacity is 13 - 2*5 = 3.

这篇关于当第一个数小于第二个数时,模数除法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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