13- integer division

Integer division
Related question:
One bucket puts n liters of water and gives several buckets. The capacity of each bucket is n liters. Each time you can pour one liter or two liters or integer liters into the bucket. ?
Insert picture description heren is the divided integer m is its addend, its addend will not exceed n
, that is n = 6 and m = 8 is meaningless. At this time, m is equivalent to 6 and at most only six numbers are added to get 6
for q (6,6) = 1 + q (6,5), that is, after dividing the expression of 6, and then adding
Insert picture description herethe proof for the fourth:
Insert picture description herecode implementation:

int fun(int n, int m)
{
        if (n < 1 || m < 1) return 0;//都等于0的时候没有划分
        if (n == 1 || m == 1)
        {
               return 1;
        }
        if (m > n)
        {
               return fun(n, n);
        }
        if (n == m)
        {
               return 1 + fun(n, n - 1);
        }
        else
               return fun(n - m, m) + fun(n, m - 1);
}
int main()
{
        cout << fun(6, 9) << endl;
        cout << fun(6, 4) << endl;
        cout << fun(6, 6) << endl;
        cout << fun(6, 1) << endl;
        return 0;
}

Results: (6, 9) and (6, 6) are the same results
Insert picture description here

Published 82 original articles · praised 7 · visits 4179

Guess you like

Origin blog.csdn.net/sunshine612/article/details/104702807