For在调用self的函数中(嵌套函数)
for in function that call it self (nested functioning)
我通过样品说出我的意见…如果我们输入像{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;
}
相关文章:
- 给定一个C++嵌套的私有结构类型,是否有从文件范围静态函数访问它的策略
- 如何在 C++ 的外部类构造函数中初始化嵌套类成员
- 如何使用 binary_search STL 函数在嵌套类中搜索整数?
- 嵌套模板函数采用 2 个具有相同内部类型的容器
- C++将结构传递给函数以访问嵌套结构
- C++ 在外部使用嵌套结构实现类函数
- 嵌套模板函数的重载
- 通过两个嵌套函数传递C++函数的名称
- 如何从另一个嵌套类中调用某个封闭类的嵌套类的函数指针成员的值?
- MSVC 2017 - 错误 - 如何将模板类 X 的模板成员函数声明为嵌套类 X::Y 的好友
- 使用嵌套函数数组是个好主意吗?
- 嵌套的模板结构构造函数实现
- C2436 '{ctor}':构造函数初始值设定项列表中的成员函数或嵌套类
- 我们可以在main函数中声明嵌套类对象吗
- C++嵌套的迭代器类(在链表类中)Insert_After函数
- 访问另一个类中的模板化嵌套类函数和对象
- Cllass 函数不解析嵌套类实例或函数
- C++析构函数嵌套类的显式模板
- C++和析构函数(嵌套类和一般问题)
- C++函数嵌套模板