当前位置:网站首页>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+rAnd0 ≤ 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)%3The 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 , WeChat :Jiabcdefh
Method 1 :QQ On-line
Method 2 : WeChat :Jiabcdefh
边栏推荐
- Wincc7.5 SP1 method for adjusting picture size to display resolution
- API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters
- Is it really hopeless to choose electronic engineering and be discouraged?
- Méthode de sauvegarde programmée basée sur la base de données distribuée elle - même
- Fault analysis | a case of MySQL remote slave database replication delay
- Can't understand kotlin source code? Starting with the contracts function~
- 7:00 tonight | application of PhD debate self supervised learning in Recommendation System
- vc hacon 联合编程 GenImage3Extern WriteImage
- Report on market demand trends and future strategic planning recommendations of the global and Chinese smart financial solutions industry 2022-2028
- The 14th five year development plan and investment prospect analysis report of China's oil and gas pipeline engineering construction 2022-2027
猜你喜欢

leetcodeSQL:602. Friend application II: who has the most friends

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

Rhca memoirs -- Introduction to cl280

基于微信电子书阅读小程序毕业设计毕设作品(4)开题报告
![[observation] Huawei's next generation data center](/img/d8/a367c26b51d9dbaf53bf4fe2a13917.png)
[observation] Huawei's next generation data center "adds momentum" to Guangxi's low-carbon and high-quality development

API call display, detailed API of Taobao, tmall and pinduoduo commodity pages, and return of APP side original data parameters

no available service ‘null‘ found, please make sure registry config correct

WinCC7.5 SP1调整画面尺寸以适应显示分辨率的方法

Wincc7.5 SP1 method for adjusting picture size to display resolution

Dacom G150双模耳机,为爱发声,呵护孩子听力健康成长
随机推荐
Leetcode topic [string]-344- reverse string
RHCA回忆录---CL280介绍
In 2021, the global fire pump drive power revenue is about $381million, and it is expected to reach $489.3 million in 2028
Basic structure of arithmetic unit
Liunx deploy Seata (Nacos version)
Wincc7.5 SP1 method for adjusting picture size to display resolution
RT-Thread 模拟器 simulator 搭建 LVGL 的开发调试环境
synchronized下的 i+=2 和 i++ i++执行结果居然不一样
Market development planning and investment prospect analysis report of Chinese government investment and financing platform 2022-2027
Equipment management - borrowing and returning module 1
腾讯云TDP-virt-viewer win客户端的软件使用
基于微信电子书阅读小程序毕业设计毕设作品(1)开发概要
基于微信电子书阅读小程序毕业设计毕设作品(7)中期检查报告
[generation confrontation network learning III] reading notes of Bigan paper and its principle understanding
Detailed explanation of yolox network structure
存储体系概述
easycode一键生成插件自定义模板
ThansmittableThreadLocal详解
Php+flash large file breakpoint continuation function sharing
Software usage of Tencent cloud TDP virt viewer win client