当前位置:网站首页>Negative remainder problem
Negative remainder problem
2022-06-12 19:31:00 【AI yuechuang】
Preface :
Hello , I'm yuechuang .
We are all familiar with the remainder of two numbers with the same symbol , But for the remainder of one positive and one negative number ?
You may feel strange , I'm brushing today leetcode7: Integer inversion (easy) When this problem , Think of this knowledge point , So I intend to discuss it .
There are two definitions of natural data remainder :
- Definition 1: If a and d It's two natural numbers ,d Nonzero , It can be proved that there are two unique integers q and r, Satisfy
a=qd+r
And0 ≤ r < d
( among q For business ,r For the remainder ). - Definition 1 It is generally used as the remainder rule in mathematics , That is, the remainder of two numbers , The remainder is always positive .
give an example :
- 5%3=3x1+2, Shang Wei 1, Remainder is 2
- (-5)%(-3)=(-3)x2+1, Shang Wei 2, Remainder is 1
- 5%(-3)=(-3)x(-1)+2, Shang Wei -1, Remainder is 2
- (-5)%3=3x(-2)+1, Shang Wei -2, Remainder is 1
- Definition 2: If a And d Is an integer ,d Nonzero , So the remainder r To satisfy such a relationship :
a = qd + r
, q Integers , And0 ≤ |r| < |d|
. Definition 2 The result of the remainder will lead to two residuals , such as5%(-3) = (-3)x(-1)+2 = (-3)x(-2)-1
, So the remainder here 2 and -1 All meet the definition . - We put 2 Called positive remainder ,-1 Become a negative remainder . Usually , When divided by d when , If the positive remainder is r1, The negative remainder is r2, So there are
r1 = r2 + d
. - All languages and calculators follow the principle of keeping the quotient as close as possible 0 Principles , namely
5%(-3)
As the result of the 2 instead of -1,(-5)%3
The result is -2 instead of 1.
The test code is as follows :
#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;
}
Explain the running results of the compiler in a popular way :
- (-5%-3) Compiler first -3 Automatically convert to a positive integer 3, And then calculate
-(5%3)
Result , So the result is -2. - (5%-3) Compiler first -3 Automatically convert to a positive integer 3, And then calculate
(5%3)
Result , So the result is 2. - (-5%3) The compiler calculates directly
-(5%3)
Result , So the result is -2.
The test results are as follows :
summary :
- Whether it is two same sign numbers or two different sign numbers , It is generally accepted in our life that the remainder result is a positive integer .
- In the compiler , The result of the remainder of two numbers with different signs depends on the sign of the molecule . negative % negative , The compiler automatically converts the negative number of the denominator to a positive integer , Then the minus sign of the negative number of the molecule is extracted , Take the remainder of two positive integers , Just add a minus sign to the final result . negative % Positive numbers , The compiler first extracts the minus sign of the negative numerator , Take the remainder of two positive integers , Add a minus sign to the final result . Positive numbers % negative , The compiler automatically converts negative denominators to positive integers , Then two positive integers are summed to get the final result .
Welcome to my official account :AI Yuechuang , There are more and more fun waiting for you to find !
official account :AI Yuechuang
AI Yuechuang · Launch coaching classes
Method 1 :QQ On-line
Method 2 : WeChat :Jiabcdefh
边栏推荐
- 基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
- easycode一键生成插件自定义模板
- "As a service", the future has come, starting from the present | new mode of it consumption, FOD billing on demand
- ThansmittableThreadLocal详解
- 王学岗room+paging3
- review. JS ppt math formula cannot be displayed
- review.js ppt数学公式无法显示
- 【图像去噪】基于各向异性滤波实现图像去噪附matlab代码
- 7:00 tonight | application of PhD debate self supervised learning in Recommendation System
- 基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
猜你喜欢
基于微信电子书阅读小程序毕业设计毕设作品(6)开题答辩PPT
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
How do I create my own appender in log4j- How to create my own Appender in log4j?
Detailed explanation of yolox network structure
io. seata. common. exception. FrameworkException: can not connect to services-server.
基于微信电子书阅读小程序毕业设计毕设作品(3)后台功能
The component style set by uniapp takes effect in H5 and app, but does not take effect in wechat applet. The problem is solved
[image denoising] image denoising based on regularization with matlab code
存储体系概述
运算器的基本结构
随机推荐
Storage system overview
ISCC2022
[notes for question brushing] line segment tree
Detailed explanation of yolox network structure
In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
In 2021, the global revenue of chlorinated polyvinyl chloride (CPVC) was about $1809.9 million, and it is expected to reach $3691.5 million in 2028
一种灵活注入 Istio Sidecar 的方案探索
Attack and defense world (WEB) -- supersqli
RT thread simulator builds lvgl development and debugging environment
Module 8 operation
unity websockt一些知识:
【数字IC/FPGA】数据累加输出
Cookie & session & kaptcha verification code
CVPR 2022 oral Dalian Institute of technology proposed SCI: a fast and powerful low light image enhancement method
How do I create my own appender in log4j- How to create my own Appender in log4j?
[image denoising] image denoising based on regularization with matlab code
存储体系概述
IO流基础知识详解--文件及IO流原理
Istio 1.14 release
review.js ppt数学公式无法显示