For在调用self的函数中(嵌套函数)

for in function that call it self (nested functioning)

本文关键字:函数 嵌套 调用 self For      更新时间:2023-10-16

我通过样品说出我的意见…如果我们输入像{a,b,c,d}这样的a序列,如果我们选择每个子序列的元素个数3,它应该是输出的…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 ... 

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

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

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

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;
}