当前位置:网站首页>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 :

  1. 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 And 0 ≤ r < d( among q For business ,r For the remainder ).
  2. 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
  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 , And 0 ≤ |r| < |d| . Definition 2 The result of the remainder will lead to two residuals , such as 5%(-3) = (-3)x(-1)+2 = (-3)x(-2)-1 , So the remainder here 2 and -1 All meet the definition .
  2. 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 .
  3. 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 :

 Insert picture description here

summary :

  1. 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 .
  2. 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

原网站

版权声明
本文为[AI yuechuang]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/163/202206121926005258.html