Recursive partitioning is easier to understand but time out
To divide n by a number m not greater than n
Total of four cases
1. If N==m has only one but to continue recursion, it is equal to 1+q (n,m-1)
2. If N<m does not consider the inability to partition then continue to use Q (n,n)
3. If there are two cases of n>m 1. Dividing the remainder with M is n-m so equals Q (n-m,m)
2. Continue dividing by a number less than M is Q (n,m-1)
3. So together is Q (n-m,m) +q (n,m-1)
4. Finally, the bottom of the recursive exit is definitely 1.
1 if (n==1 | | m==1)2 return1;
The complete code is as follows
1#include"stdio.h"2 intQintNintm)3 {4 if(n==1|| m==1)5 return 1;6 if(n<m)7 returnQ (n,n);8 if(n==m)9 return 1+q (n,m-1);Ten if(n>m) One returnQ (n-m,m) +q (n,m-1); A } - Main () - { the intN; - while(SCANF ("%d", &n)! =EOF) - { -printf"%d\n", Q (n,n)); + } -}
Integer division problem recursive version