[problem solving report] 100 cases of introduction to C language (case 4)

Keywords: C data structure Binary Search

  Zero, write in front

         This series is not updated frequently. It's interesting to see today's topic. Let's have a look. The main knowledge points are

[question 04] given a and b, ask whether a can be divided by b. The Application of if statement and conditional operatorhttps://blog.csdn.net/WhereIsHeroFrom/article/details/118198012

         Incidentally, the hero's brother's questions sometimes make complaints about the knowledge.

1, Main knowledge points

        1.if else statement and ternary operator

if(a == 0) ....
else if( a== 1) ....
else ...
return a > b ? a : b ;

        2. Data representation (supplementary knowledge points)

        Normally, our normal natural numbers are 13154, - 456456. But computers can't be represented like us, because they are binary, so positive and negative signed bits, and the highest signed bit represents positive and negative, where 0 is positive and 1 is negative.

        For positive numbers. For example, 1 is 00000001.

        What about negative numbers? For example - 1, there are three representations, which are called original code, inverse code and complement code. They are 10000001111111111011111111 respectively.

        The source code is to add a sign bit, the inverse code is to take the inverse except for the sign bit, and the complement is to take the inverse except for the sign bit and add 1. It is specified that 100000000 is the minimum negative number, which is - 2 ^ 7. (knowledge points to be tested)

2, After class exercises  

        Subtract two numbers

29. Divide two numbershttps://leetcode-cn.com/problems/divide-two-integers/         Train of thought 1

        I read brother hero's article before. The title here says you don't have to use it if you don't want to use it. As an entry-level player, just pass it, whether he lets it or not. It's over. But! But ah!!! Note that the data range of int corresponding to the above representation range is - 2 ^ 31 ~ 2 ^ 31 - 1. A negative number is 1 greater than a positive number, and it will overflow here, so just make a judgment.

int MIN_INT = -2147483648;//Minimum int variable size

int divide(int dividend, int divisor){
    if(dividend == MIN_INT)         //Judge overflow
        if(divisor == -1) return -(MIN_INT + 1);//Overflow, return specified size
    if(divisor == MIN_INT)            //Minimum divisor solution
        if(dividend == MIN_INT) return 1;
        else return 0;        
    return dividend / divisor;
}

Result analysis

Everyone is happy. How good???

Train of thought 2  

        As the saying goes, if you don't die, you will be in vain. As a player standing at the door, I still have to see how to write this question.

The first idea must be violence, enumerating from 0 to the maximum value, timeout.... wtf?

In fact, I didn't change my mind... If it times out, reduce the time complexity. The easiest thing to think of is the final result of binary search. Just do it. The first operand is converted into two negative numbers. (because of the large range of negative numbers!)  

Students who are familiar with dichotomy must know that there are left and right ranges, and then judge according to the value of the middle range to modify the range, and finally get the final result. When judging, in order to prevent data overflow, we change addition to subtraction so that it will not overflow (negative number - negative number). How can it overflow? Tell us your opinion. (I can't understand the suggestions in this part. Come back after reading brother hero's second power)

 

int MIN_INT = -2147483648; //Minimum value of int
int MAX_INT = 2147483647; //int Max

bool quickadd(int y, int z, int x){     //Judge Z * y > = x?
    int result = 0,add = y;
    while(z){        //Binary fast multiplication
        if(z & 1){
            if(result < x - add)  return false;//Ensure that result + add > = x
            result += add;
        }
        if(z != 1){
            if(add < x - add) return false; // Ensure that add + add > = x
            add += add;
        }
        z >>= 1;
    }
    return true;
}

int divide(int dividend, int divisor){

    //exception handling
    if(dividend == MIN_INT)                     //Overflow processing of divisor
        if(divisor == -1) return -(MIN_INT + 1);//2 ^ 31-1 is returned if the range of positive integer representation is exceeded
        else if(divisor == 1)   return MIN_INT; //Divide by 1 and return the corresponding value directly
    if(divisor == MIN_INT)                      //Overflow handling of divisor
        if(dividend == MIN_INT) return 1;       //Divide two numbers you want to wait and return 1
        else return 0;                          //All other cases return 0
    
    int flag = 0;//Record positive and negative
    if(dividend > 0) dividend = -dividend,flag++;//Unified processing is negative
    if(divisor > 0) divisor = -divisor,flag ++;    //Unified processing is negative
    int a = 0,b = MAX_INT,ans = 0;                 //Define left and right points and results
    while(a <= b){
        int mid = a + ((b - a) >> 1);
        if(quickadd(divisor,mid,dividend)){
            printf("d");
            ans = mid;                //Record the maximum value satisfying multiplication
            if(mid == MAX_INT) break;
            a = mid + 1;
        }
        else b = mid -1;
    }
    return flag & 1 ? -ans :ans; //Look at flag & 1 advanced. In fact, the returned result is flag%2
}

Result analysis

Make do with it..

Write at the end

In fact, I've been producing things recently, but I haven't received any feedback and suggestions. Brother hero's mode of coding together can grow for both sides. I hope you can give some suggestions and modification opinions after reading my article. If you don't understand it, you also hope to @ me in the group. I will modify the article according to the questions. This is a virtuous circle.  

This series is not updated frequently, because it is relatively basic. Many problem solutions don't feel much when they are written, so forgive me. Of course, if I see interesting questions or you have more questions, I will write an article to share with you one day. If you have any needs, you can also send a private letter or find any way you can contact me for communication. Come on, don't give up!

Posted by Mew151 on Fri, 05 Nov 2021 11:24:26 -0700