Integer division problem recursive version

Source: Internet
Author: User
Tags integer division

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.