English 中文(简体)
Problem with a recursive partition (number theory) function
原标题:

Example: Let’s say your user input is 6. Then the number of sequences that sum up to 6 is 11 (including 6 itself). The problem I am having is that it works when the user enters 1 or 6, but if not it is way off and I can t figure out why.

I can give more info on the subject if needed.

#include <iostream>

using namespace std;

int sum(double number, int min, int & counter)
{
    int temp=0, n;
    n=number+temp;

    if (number>=(n/2.0)+.5 && (number!=1))
    {
        number --;
        temp ++;
        while (number>=(n/2.0))
        {
            number --;
            temp ++;
            counter ++;
        }
    }
    else if (number==1)
    {
        counter ++;
        return 0;
    }

    sum(n-1, 1,counter);

    return 0;
}

int main()
{
    int counter=1;
    double number;

    cout << "-------------------------------------------------------
"
         << "Please enter the number: ";

    cin >> number ;
    cout << "
";

    if (number!=1)
    {
        sum(number, 1, counter);
    }


    cout << "The total number of combinations that result in a sum of "
         << number << " is: " << counter
         << "
-------------------------------------------------------
";

    return 0;
}
最佳回答

I read wiki article you pointed out, they give some instructions how to build recursive defined function. It looks different then your code. The code below works for me

#include <iostream>

using namespace std;

int sum(int k, int n)
{
    if(k == 1 || n == 1)
        return 1;

    if(k < n)
        return sum (k, k);
    else if (k == n)
        return 1 + sum (k, k-1);
    else
        return sum (k,n-1) + sum (k-n, n);
}

int main (void)
{
    int counter=1;
    double number;

    cout << "-------------------------------------------------------
"
         << "Please enter the number: ";

    cin >> number ;
    cout << "
";

    counter = sum(number, number);

    cout << "The total number of combinations that result in a sum of "
         << number << " is: " << counter
         << "
-------------------------------------------------------
";

    return 0;
}

You can test this code here

问题回答

暂无回答




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签