2014-03-31 28 views
0

所以我有这个函数和这些全局变量。在递归函数中打印C++中的累加和

int recurs=0; 
std::string sign=""; 
void count2(int k, std::vector<int> d, int total, int temp, bool flag, unsigned short int pos){ 
    std::string mas="+"; 
    std::string menos="-"; 
    if(pos==(d.size())){ 
     total+=temp; 
     if(total==k){ 
      result++; 
      std::cout << sign << "=" << k<<std::endl; 
      str=""; 
     } 
     recurs++; 
     return; 
    }  
    //Sum sign. 
    sign=sign.substr(0,sign.size()-recurs*2); 
    sign.append(mas+=std::to_string(d[pos])); 
    count2(k,d,total+temp,+d[pos],true,pos+1); 
    //Rest sign 
    sign=sign.substr(0,sign.size()-recurs*2); 
    sign.append(menos+=std::to_string(d[pos])); 
    count2(k,d,total+temp,-d[pos],false,pos+1); 
    //Append digit 
    if(flag==true) 
     count2(k,d,total,10*temp-d[pos],true,pos+1); 
    else 
     count2(k,d,total,+10*temp+d[pos],false,pos+1); 
} 

的函数被调用是这样的: count2(6,{1,2,3,3,3},0,0,true,0);

做些什么:对矢量V,它是这个组合等于第一个参数,使资金和substractions和号码,每次的组合,全局变量result增加。例如,count2(6,{1,2,3,3,3},0,0,true,0);将使result为5.由于有5种方式与这些数字相加/子6,例如:1+2+3+3-31+2-3+3+3等等。它完美的作品。另外,不使用可变字符串str

出了什么问题?没什么,但我想看看哪些组合是。我想功能打印这样想:

1+2+3+3-3 
1+2-3+3+3 
-1-2+3+3+3 
1+2+3-3+3 

问题是什么?我想知道如何正确打印使总数等于k的操作符。 如果您是run this code in ideone,它会打印:YES,这是我的实际结果。但是,是不正确的,因为没有和诸如+3或+ 3 + 3 + 3 + 3 + 3 + 3 + ....

+1+2+3+3-3=6 
+3=6 
+3-3+3-3-3+3+3=6 
+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-1+2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3=6 
+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-1+2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3=6 

正确的结果可以样子:

1+2+3+3-3 
1+2+3-3+3 
1+2-3+3+3 

〜12 + 3 + 3 + 3

回答过的问题!

+0

什么问题? –

+0

@NeilKirk如何打印正确的操作?像'1 + 2 + 3 + 3-3 1 + 2-3 + 3 + 3 -1-2 + 3 + 3 + 3 1 + 2 + 3-3 + 3等等。 –

+0

通过按值传递向量d,您正在复制它几次。这是低效的。通过const引用传递。您应该返回结果而不是修改全局变量。为什么mas和menos不是局部变量? –

回答

0

所以最后COUNT2看起来是这样的:

void count2(int k, std::vector<int> d, int total, int temp, bool flag, unsigned short int pos, std::vector<std::string> s){ 
    //std::cout << temp << " "; 
    if(pos==(d.size())){ 
     total+=temp; 
     if(total==k){ 
      result++; 
      for(unsigned short int i=0;i<d.size();i++){ 
       std::cout << s[i] << d[i]; 
      } 
      std::cout << "=" << k <<"\n"; 
     } 
     return; 
    } 
    s[pos]="+"; 
    count2(k,d,total+temp,+d[pos],true,pos+1,s); 
    //put a - sign in pos 
    s[pos]="-"; 
    count2(k,d,total+temp,-d[pos],false,pos+1,s); 
    if(pos==0) return; 
    //Append digit 
    if(flag==true){ 
     s[pos]=""; 
     //std::cout << "<0 " << 10*temp-d[pos] << " "; 
     count2(k,d,total,(10*temp-d[pos]),true,pos+1,s); 
    } 
    else{ 
     s[pos]=""; 
     //std::cout << ">0" << 10*temp+d[pos] << " "; 
     count2(k,d,total,10*temp+d[pos],false,pos+1,s); 
    } 

} 

你可以看到,而不是使用字符串变量,我使用一个字符串向量,这使得它适合递归调用。

Give it a try on ideone.com

1

如果你不介意我改变功能签名一点点,我可以建议以下?

int recurs = 0; 
void count2(int k, std::vector<int> d, int total = 0, std::string temp = "", unsigned short pos = 0) 
{ 
    if(pos == d.size()) 
    { 
     //test total number 
     if(total == k) 
     { 
      std::cout << temp <<"=" << k << std::endl; 
      recurs++; 
     } 
    } 
    else 
    { 
     //test each operator on next number in sequence 
     count2(k, d, total + d[pos], temp + ((pos) ? "+":"") + std::to_string(d[pos]), pos + 1); 
     count2(k, d, total - d[pos], temp + "-" + std::to_string(d[pos]), pos + 1); 
    } 
} 

条件运算符将从头开始删除'+'符号。默认值使该函数更容易从主或任何地方调用。通过发送temp作为string,可以更轻松地跟踪最终方程,并将其作为全局变量移除。它也消除了您的bool flag变量的需要。最后,在函数调用中更新total以从函数体中删除混乱。

+0

这个工程,但我仍然需要像123 + 45组合。一个解决方案已经被发现。 –