Recursion - Alternative addition, the number of schemes to walk the grid

Hello everyone, this is bang_bang, today I will record 2 typical recursive questions

Table of contents

1. Alternative addition

2. The number of schemes in the grid


1. Alternative addition

Alternative addition_nowcoder.com

Given two  ints A and B. Write a function that returns the value of A+B without using + or other arithmetic operators.

Test sample:

1,2

returns: 3 

Problem-solving idea: When calculating decimal addition, we will directly add two numbers, then add the result of the addition to the carry bit, and finally get the result, but we do not need to be so cumbersome after long-term training. The answer can be obtained by oral calculation, but the computer is not. It needs to calculate recursively step by step according to the rules, and the same is true for binary addition.

Adding two numbers:

1. The result of direct addition (without considering the carry), the addition without considering the carry uses XOR ( the same is 0, the difference is 1), and the carry is well ignored; a^b.

2. Get the carry result (use it as the result of the addition and then add it -> recursion), get the carry and use the AND operation to shift left one bit ; (a&b)<<1.

3. Add recursively and calculate the carry until one of the calculations is 0, and the final result is another number.

Example:

        

code show as below:

class UnusualAdd {
public:
    int addAB(int A, int B) {
        if(A==0)    return B;
        if(B==0)    return A;   //任意一个为0,则说明另一个就是最终求和结果
        int a=A^B;//不考虑进位的相加结果
        int b=(A&B)<<1;//得到进位结果
        return addAB(a,b);
    }
};

2. The number of schemes in the grid

The number of schemes in the grid

describe

Please calculate the number of n*m chessboard grids (n is the number of horizontal grids, m is the number of vertical grids) starting from the upper left corner of the chessboard and walking along the edge from the upper left corner to the lower right corner. How many ways are there in total? Go back, that is: you can only go right and down, not left and up.

Note: Walk along the edge line between the checkerboard

Data range: 1≤n,m≤8 

Enter a description:

Enter two positive integers n and m, separated by spaces. (1≤n,m≤8)

Output description:

output a row of results

Example 1

Input: 2 2

Output: 6

Problem-solving ideas:

Walking along the edge line, maybe when n=m=2, draw a picture and we can easily count how many paths there are from the starting point to the end point, but when n and m are very large, it is difficult for us to derive at this time , then in order to simplify the problem, we have to try to use recursive thinking:

1. If a grid wants to go from the upper left corner to the lower right corner, it must pass through 2 points (the upper right corner and the lower left corner, that is, the two adjacent points in the lower right corner)

2. We know that there must be only one way to walk in a straight line, that is, there is only one way to go on the border (the uppermost/leftmost side) .

3. We start from the end point and push back. The path to the end point is the sum of the paths of the two adjacent points of the end point , and the sum of the paths of the two adjacent points is the block whose position is the lower right corner. The sum of the paths of two adjacent points is recursively returned until the problem becomes the sum of the paths of multiple boundary points .

Summary: The path to the node to be sought is the sum of the paths of its adjacent 2 nodes. By recursively pushing back, the path of the adjacent 2 nodes is the sum of the paths of their adjacent 2 nodes. Until the problem becomes the sum of the paths of multiple boundary points (multiple refers to the number of boundary points that have been pushed back from the end point in total).

Code:

#include <iostream>
using namespace std;

int PathNum(int n,int m)
{
    if(n==0||m==0)
    {
        return 1;
    }
    return PathNum(n-1,m)+PathNum(n,m-1);
}

int main() {
    int n,m;
    cin>>n>>m;
    cout<<PathNum(n,m)<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/bang___bang_/article/details/131859598