简体   繁体   English

C ++简单的程序来计算步长

[英]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. 我正在编写一个C ++程序,该程序应显示步行时可能使用的所有可能步骤组合,并给出步行距离,采取的步数和最大步长。 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. 我实现了仅适用于2个步骤的算法,并且我不知道接下来要做什么,以便使其能够使用多个步骤。

#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 例如, 写了80步,但仅计算2步

And here is an example of output that I want maxsteplength=50, steps=2, distance=30 这是我想要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. 前者可以帮助您提高递归解决方案的性能,而后者可以为相同问题提供类似但不同的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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