简体   繁体   中英

C++ simple program to count steps length

I'm writing a C++ program that should show every possible combination of steps you may use when walking, given the distance to walk, number of steps to take and the maximum step length. I implemented an algorithm that works only with 2 steps, and I don't know what to do next, in order to make it work with multiple steps.

#include <iostream>

int main()
{
    int maxsteplength, steps, distance;

    std::cin >> maxsteplength >> steps >> distance;

    //for(int i=0; i<steps; i++)
    //{
        for(int j=1; j<=maxsteplength; j++)
        {
            for(int x=1; x<=maxsteplength; x++)
            {
                if(j+x==distance) std::cout << j << " " << x << std::endl;
            }
        }
    //}
    return 0;
}

In this code variable steps doesn't even work, because it always counts all the possible ways only with two steps. Eg Wrote 80 steps, but counts only with 2

And here is an example of output that I want maxsteplength=50, steps=2, distance=30

Thanks!

There are multiple solutions to your problem, but I would suggest looking into the technique that is called recursion. Basically, it defines a problem in terms of the same problem, but with a smaller size. That way, by solving the smaller instances of the same problem one can assemble the solution for the bigger problem.

For your problem, consider a recursive solution as follows.

#include <iostream>
#include <vector>

void recursive_step(int maxsteplength, int steps, int distance, std::vector<int>& vec)
{
    if(steps == 0)
    {
        if(distance == 0)
        {
            std::cout << vec[0];
            for(int x = 1; x < vec.size(); x++)
            {
                std::cout << " " << vec[x];
            }
            std::cout << std::endl;
        }
        return;
    }

    for(int x=(vec.size() > 0 ? vec.back() : 1); x <= maxsteplength; x++)
    {
        if(distance - x >= 0)
        {
            vec.push_back(x);
            recursive_step(maxsteplength, steps-1, distance - x, vec);
            vec.pop_back();
        }
    }
}

int main()
{
    int maxsteplength, steps, distance;

    std::cin >> maxsteplength >> steps >> distance;
    std::vector<int> vec;
    recursive_step(maxsteplength, steps, distance, vec);
    return 0;
}

Note that this solution is unbelievably inefficient and can fail you for large input sizes, especially for large maxsteplength and large distances. I would recommend using this solution as a prototype and working on your own, better and more efficient recursive solution.

Once you got the hang of recursion, and can solve this problem using recursion, you may look into the concept of memoization and even into dynamic programming. The former can help you increase the performance of your recursive solution while the latter can present a similar, yet different solution to the same problem.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM