1#include <iostream>2 using namespacestd;3 4 //Print a Division5 voidDisplayint*result,intlength)6 {7cout << result[0];8 for(inti =1; i < length; i++)9 {Tencout <<'+'<<Result[i]; One } Acout <<Endl; - } - the //Fun : You can output an expression, you can also calculate the number of expressions - //n is the integer to be divided, M is the maximum addend upper limit; - //recursive solution: The divide is divided into five kinds of case discussion; set F (n,m) to n m number - intDivideintNintMint*result,intlength) + { - //n >= 0 && m = = 1 o'clock, with N in {,.... 1}. is divided into F (n-1,m); Example: 6 = 1+1+1+1+1+1 + if(N >=0&& m = =1) A{//until (n = = 0), and M = = 1 o'clock output. at if(n = =0&& m = =1) - display (result, length); - Else - { -Result[length] =1;//Otherwise, the split expression is written to result -Divide (n1, m, result, length+1);//recursive Add in } - return 1;//An expression equivalent to a single split to } + - //when n = = 1 && m > 1 o'clock, the decomposition has been completed, the output is as follows: 4+2+1 the Else if(n = =1&& m >1) * { $Result[length] =N;Panax NotoginsengDisplay (result, length+1); - return 1;//return 1 types of partitioning possible the } + A //when n < m, because there is no negative division, it is equivalent to F (n,n), such as: when N>m, F (n-m,m) divided into F (m,m) Division the Else if(N <m) + returndivide (n, n, result, length); - $ //When n = = m, it is divided into: 1. When n is included, only {n}; $ //2. When n is not included, the maximum number in the division must be smaller than n, i.e. all n-1 of n are divided. Synthetic: F (n,n) = f (n, n-1) + 1; - Else if(n = =m) - { theResult[length] = m;//Add Division -Display (result, length+1);//Output PartitioningWuyi returnDivide (n, n1, result, length) +1;//Recursive the } - Wu //when n > M > 1 o'clock, divided into: 1. With M, {m, {x1,x2,.. XI}}, {x1,.. XI} and for n-m, i.e. F (n-m,m)-the case where the n<m condition is invoked - //2. When no m is included, all values in the division are smaller than m, i.e. the m-1 of n is divided into F (n, m-1); About //synthetic: F (n,m) = f (n-m, M) + f (n, m-1); $ Else if(N >m) { -Result[length] =m; - returnDivide (n-m, M, result, length+1) + Divide (n, M-1, result, length); - } A } + the intMainvoid) - { $ Const intMAXN =1010; the intN, buf[maxn], length =0; theCIN >>N; theDivide (n, n1, buf, length);//Division of F (n,n-1) the - return 0; in}
Integer division _ outputs all divisions and calculates the number of divisions