【问题标题】:for in function that call it self (nested functioning)for in 函数调用它自己(嵌套函数)
【发布时间】:2015-09-08 04:21:31
【问题描述】:

我通过样本告诉我我的意见...... 如果我们输入像 {a,b,c,d} 这样的 a 序列,并且如果我们选择每个子序列的元素数 3 它应该是 out output ... a,b,c | a,b,d | a,c,d | b,c,d ....注意排列很重要(我正在使用数组)它的某种输出包含序列的 3 个元素的整个子集 ....

#include <iostream>
#include <conio.h>
#include <windows.h>

int p = 0, q = 0, i;
void allinone(int thing[], int n, int sub, int tek[], int sum[])
{
    for (i = p;i <= n - sub + p;i++)
    {
        tek[q] = thing[i];
        if (p < sub - 1)
        {
            ++q;
            ++p;
            allinone(thing, n, sub, tek, sum);
        }
        if (q == (sub - 1))
        {
            for (auto out_p = 0;out_p <= p;out_p++)
                std::cout << tek[out_p] << "\t";
            std::cout << "\n";
        }
    }
    --q; 
    return; // not need
}
int main()
{
    constexpr int n=5, sub=3; //delete the constexpr if ur compiler is mingw
    /*std::cout << "enter array len : "; //add this if ur compiler is mingw
    std::cin >> n; // number of elements in sequence
    std::cout << "enter sub that u want: ";
    std::cin >> sub;*/ // subset 
    int sum[sub], tek[sub], thing[n];
    for (i = 0;i <= n - 1;i++)
        std::cin >> thing[i];
    allinone(thing, n, sub, tek, sum);
    _getch();
    return 0;
}

是哪一部分让程序无法返回上一个函数...

请看下面的步骤...

我认为我认为诺言的问题

#allinone1
first for pormise me it will do this for the i=0,1    
{
doing for i=1{
sequence{a,b,c,d}   thing[0]=a,thing[1]=b,thing[2]=c,thing[3]=d
tek[0] = a ... go into if ... p=1 , q=1 ... and again its going into the #allinone2 ... 
now in the for its promise me to do this for i=1,2
{
doing for i=1
tek[1]= b ... go into if ... p=2 ,q=2 ... and again its going to #allinone3
another for its promise me to do this with i=2,3 
{
doing for i=2 
tek[2]= c .. go into the output if ... output a b c 
doing for i=3
tek[2]= d .. go into output if ... output a d d 
}get out of for 
q=1
end of the allinone3 
go into allinone 2 for 
q is not 2 ... 
doing for i=2 // i think this part made the problem ....
and ... 

我认为它没有存储最后一个 i ......

【问题讨论】:

    标签: c++ nested nested-loops


    【解决方案1】:

    我看到的主要问题是您正在修改mainallinone 中的全局变量i

    在处理递归函数时,最好将用于终止递归的变量的值作为参数传递给函数。

    我稍微更新了您的代码以使递归更进一步。然而,它并不完美。它产生如下输出:

    2 3 3
    

    我会让你弄清楚最后一点。


    #include <iostream>
    
    // Don't use global variables for variables that are used
    // to terminate recursion.
    // int p = 0, q = 0, i;
    
    void allinone(int thing[], int n, int p, int sub, int tek[], int sum[])
    {
        int q = p;
        for (int i = p; i <= n - sub + p; i++)
        {
            tek[q] = thing[i];
            if (p < sub - 1)
            {
               allinone(thing, n, p+1, sub, tek, sum);
            }
            if (q == (sub - 1))
            {
                for (auto out_p = 0;out_p <= p;out_p++)
                    std::cout << tek[out_p] << "\t";
                std::cout << "\n";
            }
        }
    }
    
    
    int main()
    {
        constexpr int n=5, sub=3; //delete the constexpr if ur compiler is mingw
        /*std::cout << "enter array len : "; //add this if ur compiler is mingw
        std::cin >> n; // number of elements in sequence
        std::cout << "enter sub that u want: ";
        std::cin >> sub;*/ // subset 
        int sum[sub], tek[sub], thing[n];
        for (int i = 0;i <= n - 1;i++)
            std::cin >> thing[i];
        allinone(thing, n, 0, sub, tek, sum);
        return 0;
    }
    

    【讨论】:

    • 我知道 p 和 q 在我的代码中是重复的,但我需要 p 用于另一部分,我没有在其中写...我实际上不明白反对票..
    • 我真的没有得到你的第一行
    • @Parano,全局变量通常是有问题的。如果您能弄清楚如何避免使用它们,这将对您有所帮助。
    • @Parano,你说的是注释掉的行// int p = 0, q = 0, i;吗?
    • 我的意思是... int q=p ;
    猜你喜欢
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多