Integer division recursive algorithm, and all the divided output (Java)

Integer division recursive algorithm, and all the divided output (Java)

Hit advertising , all the algorithms work I will update the code on my GitHub
slip Knowledge Factory --Homework-AlgorithmAnalysis

I hope we can play a little ⭐⭐ , thank you

Algorithm is still working, this is an integer divide problem.

The Internet for a long time, most are seeking integer division of the number of species, rarely seeking detailed breakdown situation.

There are also C language version, and can not find a Java.

So I came up with a Java (almost the same, but if someone needs Java version of the easy to search)

problem

Explain the problem, what is the integer division?

n = m1 + m2 + ... + mi; (where mi is a positive integer and 1 <= mi <= n), then the {m1, m2, ..., mi} is a division of n.

If {m1, m2, ..., mi} is not more than the maximum value m, i.e., max (m1, m2, ..., mi) <= m, it is said to belong to a m n divided. Here we denote the number n is divided by m f (n, m);

For example, when n = 6 we can get that several of the following division:
6 = 6
6 5 + 1 =
6 2 = 4 +
6 + 4 = 1 + 1
6 3 + 3 =
6 + 2 + 1 = 3
6 3 + 1 = 1 + 1 +
6 2 + 2 + 2 =
6 = 2 + 2 + 1 + 1
6 + 1 2 = 1 + 1 + 1 +
6 = 1 + 1 + 1 + 1 + 1 + 1

analysis

This way it is very easy to see, n for an integer division is based on n-1 based on, so that there may be recurrence formula.

Difficult to find a single parameter recursive, so we add a parameter m, a division result represents the largest addend.

The relationship between n and m, consider the following cases:

  1. When n = 1, regardless of the value of the number m (m> 0), i.e., only one partitioning {1};

  2. When m = 1, n has a value regardless of the number, n, of only one division 1, {1,1,1, ..., 1};

  3. When n = m, divided according to whether to include n, it can be divided into two situations:

    (A) a case containing n division, i.e., only a {n};

    (B) n does not include the division, the largest number of time division certainly smaller than n, i.e. all n (n-1) division. Therefore q (n, n) = 1 + q (n, n-1);

  4. When n <m, since the negative division impossible, therefore equivalent to q (n, n);

  5. However, n> m, the maximum value according to division contains m, it can be divided into two situations:

    (A) contained in the case of division of m, i.e., nm {m, {x1, x2, ... xi}}, where {x1, x2, ... xi} and is, therefore this case is q (nm, m)

    The case (b) does not contain m division, the division of all values ​​smaller than m, i.e., the n (m-1) is divided, the number of q (n, m-1);

Therefore q (n, m) = q (nm, m) + q (n, m-1);
Here Insert Picture Description

Code

Seeking integer division species

/*
     * @Title division
     * @Description 整数划分方法
     * @author 滑技工厂
     * @Date 2020/3/8
     * @param [n, m]   n->要划分的整数  m->最大加数
     * @return int 输出有多少种划分
     * @throws
     */
    public static int division(int n, int m) {
        if (n < 1 || m < 1)
            return 0;
        if (n == 1 || m == 1)
            return 1;
        //最大加数如果大于n 则另最大加数为n
        if (n < m)
            return division(n, n);
        //m=n,则从
        if (n == m)
            return division(n, m - 1) + 1;

        return division(n, m - 1) + division(n - m, m);

    }

Seeking specific division of


	static int mark[] = new int[100];//记录分解情况
    static int n;
    /*
     * @Title divide
     * @Description 输出划分
     * @author 滑技工厂
     * @Date 2020/3/8
     * @param [now, k, pre]
     * @return void
     * @throws
     */
    public static void divide(int now, int k, int pre) {
        int i;
        //数组长度大于n就返回
        if (now > n) return;

        if (now == n) {

            System.out.printf("%d=", n);
            for (i = 0; i < k - 1; i++) {
                System.out.printf("%d+", mark[i]);
            }
            System.out.printf("%d\n", mark[i]);
        } else {
            for (i = pre; i > 0; i--) {
                if (i <= pre) {
                    mark[k] = i;
                    now += i;
                    divide(now, k + 1, i);
                    now -= i;
                }
            }
        }
    }

Results 6 as an example, is obtained
Here Insert Picture Description

End Sahua ✿, gifts of roses.

Here Insert Picture Description

Published 41 original articles · won praise 94 · views 9557

Guess you like

Origin blog.csdn.net/qq_41718454/article/details/104733740