如何使用递归函数来反转另一个递归函数的输出?

时间:2015-11-30 16:53:41

标签: c++ function recursion

我为类编写了一个赋值,我必须编写一个递归函数来使用递归输出一组数字,非常简单,我设法做到了。现在我必须编写另一个函数,使用递归以相反的顺序输出相同的数字,这就是我被卡住的地方,我对如何使用递归打印出反向输出感到茫然。所以我的问题仍然存在,如何使用递归打印出我的第一个函数的反向输出?我应该颠倒输出的顺序,而不是反过来计算序列。

另外,另一个问题,为什么我的计数器不工作?谢谢你的帮助。

代码:

#include <iostream>
using namespace std;

void reverseOJP(int refD, int D) {

// ?


}

void lengthOJP (int D, int count) {

cout << endl << "The length of the OJP for " << D << " is " << count << endl;

}

int OJP(int D, int count) {

count++;
if (D == 1) { // Base case

    return count;


} else if ((D % 2) != 0) { // odd numbers

    D = ((D * 3) + 1); // if D is odd

    cout << D << " ";

    OJP(D, count); // Recursive Call

} else { // even

    D /= 2;

    cout << D << " ";

    OJP(D, count);
}

return count;
}

int main() {
int D, count = 0; // variables
int refD;

cout << "Positive integer: " << endl;

D = 12;

// OJP output
cout << "The OJP for " << D << endl;
cout << D << " ";

refD = D; // D reference for output purposes
//OJP(D, count, refD); // OJP call

count = count + OJP (D, count);

cout << "The OJP for " << D << endl;
cout << "1 ";
reverseOJP(refD, D); // call and print reverse OJP
lengthOJP(refD, count); // Once reverse is printed print length


return 0;
}

期望的输出:

Enter a positive integer: 12

The OJP for 12: 12 6 3 10 5 16 8 4 2 1

The reverse OJP for 12: 1 2 4 8 16 5 10 3 6 12 

The length of the OJP for 12 is 10

2 个答案:

答案 0 :(得分:0)

要反转输出,只需重新排序int OJP(int D, int count)

打印当前号码,而不是之前打印 递归调用(即返回指令之前):

void reverseOJP(int D, int refD) {
  if (D == 1) { // Base case
    return ;
  } else if ((D % 2) != 0) { // odd numbers
    D = ((D * 3) + 1); // if D is odd

    reverseOJP(D, D); // Recursive Call
    cout << refD << " ";

 } else { // even
    D /= 2;
    reverseOJP(D, D);
    cout << refD << " ";
 }

}

列出的代码只是稍微改编自OJP的复制粘贴,尚未优化。

答案 1 :(得分:0)

至于返回的计数,请查看下面的示例输出,看看是否可以找到与...#count; count&#34;在main()比你预期的。当你可以看到代码正在做什么时,更容易解决这类问题,我喜欢在我的代码中添加一些调试打印,如下所示:

#include <iostream>
#include <string>  // added for debug stuff.
using namespace std;
using namespace std;
void reverseOJP(int refD, int D) {
// ?
}

void lengthOJP (int D, int count) {
   cout << endl << "The length of the OJP for " << D << " is " << count << endl;
}



//***** debug stuff ******
int debug_depth = 0; // caller must increment & decrement when entering & leaving funct.

void debug_indent( ) {
   for( int i = 0; i < debug_depth; ++i ) cout << ":   ";
}

void debug_enter( const string & funct_name ) {
   ++debug_depth;
   debug_indent(); cout << ">" << funct_name << "()" << endl;
}

void debug_exit( const string & funct_name ) {
   --debug_depth;
   debug_indent(); cout << "<" << funct_name << "()" << endl;
}
//***** end stuff ******

int OJP(int D, int count) {
   // debug output convention, ">" going into a fnct and "<" leaving it.
   // doing the simple indent makes it easier to see what fnct is doing.
   debug_enter("OJP");
   debug_indent(); cout << "OJP(): my count=" << count << endl;

   count++;
   debug_indent(); cout << "OJP(): after ++, now count=" << count << endl;

   if (D == 1) { // Base case
       debug_indent(); cout << "debug.OJP(): D==1, returning count=" << count << endl;
       debug_exit("OJP");
       return count;
   } else if ((D % 2) != 0) { // odd numbers
       D = ((D * 3) + 1); // if D is odd
       // SAVE:  cout << D << " ";
       debug_indent(); cout << "odd, now D=" << D << endl;
       OJP(D, count); // Recursive Call
   } else { // even
       D /= 2;
       // SAVE: cout << D << " ";
       debug_indent(); cout << "even, now D=" << D << endl;
       OJP(D, count);
   }
   debug_indent(); cout << "debug.OJP(): returning count=" << count << endl;
   debug_indent(); cout << "debug.OJP(): returning count=" << count << endl;
   debug_exit("OJP");

   // *** When you return count here, what is the value going back to main() ?
   return count;
}

int main() {
   int D, count = 0; // variables
   int refD;
   cout << "Positive integer: " << endl;
   D = 12;
   // OJP output
   cout << "The OJP for " << D << endl;
   cout << D << " ";
   refD = D; // D reference for output purposes
   //OJP(D, count, refD); // OJP call
   cout << "main(): before OJP(), count=" << count << endl;
   count = count + OJP (D, count);
   cout << "main(): now, count=" << count << endl;
   cout << "The OJP for " << D << endl;
   cout << "1 ";

   reverseOJP(refD, D); // call and print reverse OJP
   lengthOJP(refD, count); // Once reverse is printed print length
   return 0;
}

示例输出:

Positive integer: 
The OJP for 12
12 debug.main(): before OJP(), count=0
:   >OJP()
:   OJP(): my count=0
:   OJP(): after ++, now count=1
:   even, now D=6
:   :   >OJP()
:   :   OJP(): my count=1
:   :   OJP(): after ++, now count=2
:   :   even, now D=3
:   :   :   >OJP()
:   :   :   OJP(): my count=2
:   :   :   OJP(): after ++, now count=3
:   :   :   odd, now D=10
:   :   :   :   >OJP()
:   :   :   :   OJP(): my count=3
:   :   :   :   OJP(): after ++, now count=4
:   :   :   :   even, now D=5
:   :   :   :   :   >OJP()
:   :   :   :   :   OJP(): my count=4
:   :   :   :   :   OJP(): after ++, now count=5
:   :   :   :   :   odd, now D=16
:   :   :   :   :   :   >OJP()
:   :   :   :   :   :   OJP(): my count=5
:   :   :   :   :   :   OJP(): after ++, now count=6
:   :   :   :   :   :   even, now D=8
:   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   OJP(): my count=6
:   :   :   :   :   :   :   OJP(): after ++, now count=7
:   :   :   :   :   :   :   even, now D=4
:   :   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   :   OJP(): my count=7
:   :   :   :   :   :   :   :   OJP(): after ++, now count=8
:   :   :   :   :   :   :   :   even, now D=2
:   :   :   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   :   :   OJP(): my count=8
:   :   :   :   :   :   :   :   :   OJP(): after ++, now count=9
:   :   :   :   :   :   :   :   :   even, now D=1
:   :   :   :   :   :   :   :   :   :   >OJP()
:   :   :   :   :   :   :   :   :   :   OJP(): my count=9
:   :   :   :   :   :   :   :   :   :   OJP(): after ++, now count=10
:   :   :   :   :   :   :   :   :   <OJP()
:   :   :   :   :   :   :   :   :   debug.OJP(): returning count=9
:   :   :   :   :   :   :   :   <OJP()
:   :   :   :   :   :   :   :   debug.OJP(): returning count=8
:   :   :   :   :   :   :   <OJP()
:   :   :   :   :   :   :   debug.OJP(): returning count=7
:   :   :   :   :   :   <OJP()
:   :   :   :   :   :   debug.OJP(): returning count=6
:   :   :   :   :   <OJP()
:   :   :   :   :   debug.OJP(): returning count=5
:   :   :   :   <OJP()
:   :   :   :   debug.OJP(): returning count=4
:   :   :   <OJP()
:   :   :   debug.OJP(): returning count=3
:   :   <OJP()
:   :   debug.OJP(): returning count=2
:   <OJP()
:   debug.OJP(): returning count=1
<OJP()
debug.main(): now, count=1
The OJP for 12
1 
The length of the OJP for 12 is 1