当前位置:网站首页>负数取余问题
负数取余问题
2022-06-12 19:26:00 【AI悦创】
前言:
你好,我是悦创。
对于两个相同符号的数取余大家都很熟悉,但是对于一正一负的数取余呢?
大家可能觉得会很陌生,今天在刷 leetcode7:整数反转(easy)这道题的时候,想起了这个知识点,遂打算探讨一番。
自然数取余定义分为两种:
- 定义1:如果 a 和 d 是两个自然数,d 非零,可以证明存在两个唯一的整数 q 和 r,满足
a=qd+r且0 ≤ r < d(其中 q 为商,r 为余数)。 - 定义1一般作为数学中的取余法则,即两个数取余,余数总是为正数。
举例:
- 5%3=3x1+2,商为1,余数为 2
- (-5)%(-3)=(-3)x2+1,商为 2,余数为 1
- 5%(-3)=(-3)x(-1)+2,商为 -1,余数为 2
- (-5)%3=3x(-2)+1,商为 -2,余数为 1
- 定义2:如果 a 与 d 是整数,d 非零,那么余数 r 满足这样的关系:
a = qd + r, q 为整数,且0 ≤ |r| < |d|。定义 2 取余的结果就会导致出现两个余数了,比如5%(-3) = (-3)x(-1)+2 = (-3)x(-2)-1,所以这里的余数 2 和 -1 都满足定义。 - 我们把 2 称为正余数,-1 成为负余数。通常,当除以 d 时,如果正余数为 r1,负余数为 r2,那么有
r1 = r2 + d。 - 所有语言和计算器都遵循了尽量让商尽量靠近 0 的原则,即
5%(-3)的结果为 2 而不是 -1,(-5)%3的结果是 -2 而不是 1。
测试代码如下:
#include <iostream>
using namespace std;
int main()
{
cout<<(5%3)<<endl; //5=3x1+2
cout<<(-5%-3)<<endl; //-5=-3x1-2
cout<<(5%-3)<<endl; //5=-3x(-1)+2
cout<<(-5%3)<<endl; //-5=3x(-1)-2
system("pause");
return 0;
}
通俗地解释编译器的运行结果:
- (-5%-3) 编译器先将 -3 自动转换为正整数 3,然后计算
-(5%3)的结果,所以运算结果为 -2。 - (5%-3) 编译器先将 -3 自动转换为正整数 3,然后计算
(5%3)的结果,所以运算结果为 2。 - (-5%3) 编译器直接计算
-(5%3)的结果,所以运算结果为 -2。
测试结果如下:

总结:
- 不管是两个同号数取余还是两个异号数取余,取余结果是正整数才是我们生活中普遍认可的结果。
- 在编译器中,两个异号的数取余之后的结果取决于分子的符号。负数%负数,编译器会将分母的负数自动转换为正整数,然后再将分子负数的负号提取出来,将两个正整数取余,最后的结果加上负号就好了。负数%正数,编译器先将分子负数的负号提取出来,将两个正整数取余,最后结果加上负号即可。正数%负数,编译器自动将分母负数转换为正整数,然后两个正整数取余得到就是最终结果。
欢迎关注我公众号:AI悦创,有更多更好玩的等你发现!
公众号:AI悦创
AI悦创·推出辅导班,微信:Jiabcdefh
方法一:QQ在线
方法二:微信:Jiabcdefh
边栏推荐
- Details of thansmitablethreadlocal
- 一种灵活注入 Istio Sidecar 的方案探索
- Wincc7.5 SP1 method for adjusting picture size to display resolution
- Tarfile decompress nested tar
- Shell programming regular expressions and metacharacters
- Rhca memoirs -- Introduction to cl280
- leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]
- 超级重磅!Apache Hudi多模索引对查询优化高达30倍
- Embedded development: 6 necessary skills for firmware engineers
- no available service ‘null‘ found, please make sure registry config correct
猜你喜欢

【图像去噪】基于正则化实现图像去噪附matlab代码

Kali implements port forwarding through iptables

Add, delete, modify and query mysql, common MySQL commands

io.seata.common.exception.FrameworkException: can not connect to services-server.

Business opportunities with an annual increase of 3billion - non cage eggs or a new blue ocean for export to ASEAN

Redis (XXXII) - using redis as a distributed lock

vc hacon 联合编程 GenImage3Extern WriteImage

The Bean Validation API is on the classpath but no implementation could be found

Chrome browser solves cross domain problems

How do I create my own appender in log4j- How to create my own Appender in log4j?
随机推荐
【数字IC/FPGA】数据累加输出
New product launch
觀察網站的頁面
A journey of database full SQL analysis and audit system performance optimization
Jenkins各配置选项介绍原创
mysql的增删改查,mysql常用命令
Leetcode 494. Objectives and
Redis (XXXII) - using redis as a distributed lock
io.seata.common.exception.FrameworkException: can not connect to services-server.
Market development planning and investment prospect analysis report of Chinese government investment and financing platform 2022-2027
leetcode:6096. Success logarithm of spells and potions [sort + dichotomy]
[0008] unordered list
Leetcode topic [string]-344- reverse string
什么是数据驱动
Research Report on the overall scale, major manufacturers, major regions, products and application segments of lifeboats in the global market in 2022
合理地配置线程池
Details of thansmitablethreadlocal
3GPP RAN第一次F2F会议,都干了些啥?
leetcode:5259. Calculate the total tax payable [simple simulation + see which range]
[SQL] MySQL query statement execution sequence analysis