Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
退出c+中的递归void方法+; 我有一个用C++编写的递归空隙方法。当我得到所需的值时,如何退出这个递归方法 #include<bits/stdc++.h> using namespace std; int a[25]; char x[25]; int n, m ; string s; void go(int pos) { if(pos == n - 1) { int suma = a[0]; for(int i = 0 ; i < n - 1;i++) { if(x[i] == '+')suma += a[i + 1]; else suma -= a[i + 1]; } //here I need hint how to close this method when suma will equal to m //if(suma == x) here I should break this method return ; } x[pos] = '+'; go(pos+1); x[pos] = '-'; go(pos+1); } main() { cin >> n >> m; for(int i = 0 ; i < n;i++) cin >> a[i]; go(0); for(int i = 0 ; i < n- 1;i++) cout << x[i]<<" "; } #包括 使用名称空间std; INTA[25]; charx[25]; int n,m; 字符串s; 无效go(内部位置) { 如果(位置==n-1) { int suma=a[0]; 对于(int i=0;i>n>>m; 对于(int i=0;i>a[i]; go(0); 对于(int i=0;in>>m; 对于(int i=0;i>a[i]; 试一试{ go(0); } catch(result_found&){ 对于(int i=0;i_C++ - Fatal编程技术网 n>>m; 对于(int i=0;i>a[i]; go(0); 对于(int i=0;in>>m; 对于(int i=0;i>a[i]; 试一试{ go(0); } catch(result_found&){ 对于(int i=0;i,c++,C++" /> n>>m; 对于(int i=0;i>a[i]; go(0); 对于(int i=0;in>>m; 对于(int i=0;i>a[i]; 试一试{ go(0); } catch(result_found&){ 对于(int i=0;i,c++,C++" />

退出c+中的递归void方法+; 我有一个用C++编写的递归空隙方法。当我得到所需的值时,如何退出这个递归方法 #include<bits/stdc++.h> using namespace std; int a[25]; char x[25]; int n, m ; string s; void go(int pos) { if(pos == n - 1) { int suma = a[0]; for(int i = 0 ; i < n - 1;i++) { if(x[i] == '+')suma += a[i + 1]; else suma -= a[i + 1]; } //here I need hint how to close this method when suma will equal to m //if(suma == x) here I should break this method return ; } x[pos] = '+'; go(pos+1); x[pos] = '-'; go(pos+1); } main() { cin >> n >> m; for(int i = 0 ; i < n;i++) cin >> a[i]; go(0); for(int i = 0 ; i < n- 1;i++) cout << x[i]<<" "; } #包括 使用名称空间std; INTA[25]; charx[25]; int n,m; 字符串s; 无效go(内部位置) { 如果(位置==n-1) { int suma=a[0]; 对于(int i=0;i>n>>m; 对于(int i=0;i>a[i]; go(0); 对于(int i=0;in>>m; 对于(int i=0;i>a[i]; 试一试{ go(0); } catch(result_found&){ 对于(int i=0;i

退出c+中的递归void方法+; 我有一个用C++编写的递归空隙方法。当我得到所需的值时,如何退出这个递归方法 #include<bits/stdc++.h> using namespace std; int a[25]; char x[25]; int n, m ; string s; void go(int pos) { if(pos == n - 1) { int suma = a[0]; for(int i = 0 ; i < n - 1;i++) { if(x[i] == '+')suma += a[i + 1]; else suma -= a[i + 1]; } //here I need hint how to close this method when suma will equal to m //if(suma == x) here I should break this method return ; } x[pos] = '+'; go(pos+1); x[pos] = '-'; go(pos+1); } main() { cin >> n >> m; for(int i = 0 ; i < n;i++) cin >> a[i]; go(0); for(int i = 0 ; i < n- 1;i++) cout << x[i]<<" "; } #包括 使用名称空间std; INTA[25]; charx[25]; int n,m; 字符串s; 无效go(内部位置) { 如果(位置==n-1) { int suma=a[0]; 对于(int i=0;i>n>>m; 对于(int i=0;i>a[i]; go(0); 对于(int i=0;in>>m; 对于(int i=0;i>a[i]; 试一试{ go(0); } catch(result_found&){ 对于(int i=0;i,c++,C++,您可以在方法上使用“静态布尔”标志。 函数上的静态字段在几个函数调用之间保持其值 void go(int pos) { static bool found=false; if (found) return; //rest of your code here... //of course when you found what are you searching, do found = true; } 在找到结果的地方抛出,并在最初的调用站点捕获 #inclu

您可以在方法上使用“静态布尔”标志。 函数上的静态字段在几个函数调用之间保持其值

void go(int pos)
{
    static bool found=false;
    if (found) return;

    //rest of your code here...
    //of course when you found what are you searching, do found = true;

}

在找到结果的地方抛出,并在最初的调用站点捕获

#include <string>
#include <iostream>

int a[25];
char x[25];
int n, m ;
std::string s;

struct result_found{};

void go(int pos)
{
    if(pos == n - 1)
    {
        int suma = a[0];
        for(int i = 0; i < n - 1; i++)
        {
            if(x[i] == '+')suma += a[i + 1];
            else suma -= a[i + 1];
        }
        if (suma == m)
            throw result_found{};
    }
    x[pos] = '+';
    go(pos+1);
    x[pos] = '-';
    go(pos+1);
}

main()
{
    std::cin >> n >> m;
    for(int i = 0 ; i < n;i++)
        std::cin >> a[i];
    try {
        go(0);
    }
    catch (result_found&) {}
    for(int i = 0 ; i < n- 1;i++)
        std::cout << x[i] << " ";
}
#包括
#包括
INTA[25];
charx[25];
int n,m;
std::字符串s;
结构结果_找到{};
无效go(内部位置)
{
如果(位置==n-1)
{
int suma=a[0];
对于(int i=0;i>n>>m;
对于(int i=0;i>a[i];
试一试{
go(0);
}
catch(result_found&){
对于(int i=0;istd::cout if(X==suma)返回;Did
if(X==suma)
对你不起作用?Benjamin它再次适用于递归。在我看来,你可以创建
go
a
bool
函数,并使用它来知道递归调用后是否继续。什么是
n
x
?全局?我认为bool的全局声明在这种情况下也有帮助。好的建议谢谢s@JakhongirSabirov最好使用局部变量来避免污染全局名称空间。当然,你正在编写有竞争力的编程代码,所以你不必在意:)这里使用
static
意味着bool不会分配到堆上,除非你计划在程序完成之前使用它,否则你不应该这样做。这意味着你必须管理内存按年。使用内部退出条件会更好。打破递归函数的非常奇怪的方法,当您再次调用该函数时会发生什么?只需从function@AlanBirtles你说得对!我只是想用最短的答案得到一个AC