在 C++ 中使用递归的反向数组

reverse array using recursion in c++

本文关键字:数组 递归 C++      更新时间:2023-10-16

赋值相对简单,使用ReverseStringRecursive函数反转数组main。但是,限制是我只能使用单个intchar而不能使用其他声明变量的内容(这包括禁止for循环等)。此外,我只能使用iostreamconio.h的额外库。我遇到的问题是,当我只需要向后打印时,string将向前打印,然后向后打印。reverseMe变量指向包含"abcdefghijklmnopqrstuvwxyz"main中的string。这个函数不是假设打印string只是反转然后main将打印字符串。

// INCLUES AND NAMESPACES
#include <iostream>
#include<conio.h>
using namespace std;
// CONSTANTS
const int STRING_SIZE = 100;
// PROTOTYPES
int ReverseStringRecursive(char*);
// MAIN
int main() {
// create a string
char someString[STRING_SIZE] = "abcdefghijklmnopqrstuvwxyz";
// display the string before being reversed
cout << "The string contains: " << endl;
cout << someString << endl << endl;
// make the call to the recursive function
cout << "CALL THE REVERSING FUNCTION" << endl << endl;
ReverseStringRecursive(someString);
// display the string after being reversed
cout << "The string contains: " << endl;
cout << someString << endl;
// exit program
_getch();
return 0;
}
int ReverseStringRecursive(char* reverseMe) {
// YOUR IMPLEMENTATION GOES HERE...
int position = 0;
char holder = ' ';
if (reverseMe[0] == '') {
return 1;
}
else {
holder = reverseMe[position];
}
ReverseStringRecursive(reverseMe + 1);
while (reverseMe[position] != '') {
position++;
}
reverseMe[position] = holder;
return position;
}

我得到的示例输出:

"abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba"

我应该得到什么:

"zyxwvutsrqponmlkjihgfedcba"

棘手的问题。 您必须缩短每次递归的内部字符串,方法是在调用递归函数之前在最后一个字符上放置"\0",然后在递归调用后执行交换。

算法:

0. save the index of the last character in the string
1. Save the last character of the current string
2. Set the last character of the current string to null (use the saved index)
3. Call the recursive function starting one character in which will recurse the algorithm for the next inner string (we have already shortened the end of the recursed string)
4. Once the recursion has finished, set the last character to the first char of the current string; then
5. set the first character of the current string to the saved character (which was at the end)

这也适用于奇数长度的字符串。

以下代码应该适用于窗口系统。 要使其在Linux上运行,只需注释掉conio.h包含行,注释__getch()行并取消注释cin.getch()行。

// INCLUES AND NAMESPACES
#include <iostream>
#include <conio.h>
using namespace std;
// CONSTANTS
const int STRING_SIZE = 100;
// PROTOTYPES
int ReverseStringRecursive(char *);
char *orig;
// MAIN
int main()
{
// create a string
char someString[STRING_SIZE] = "abcdefghijklmnopqrstuvwxyz";
orig = someString;
// display the string before being reversed
cout << "The string contains: " << endl;
cout << someString << endl << endl;
// make the call to the recursive function
cout << "CALL THE REVERSING FUNCTION" << endl << endl;
ReverseStringRecursive(someString);
// display the string after being reversed
cout << "The string contains: " << endl;
cout << someString << endl;
// exit program
_getch();        // uncoment conio.h on a windows system
//  std::cin.get();  // use this if on a linux system
return 0;
}
int ReverseStringRecursive(char *reverseMe)
{
int last_index = 0;
while (reverseMe[last_index + 1] != '')
last_index++;
char save_char = reverseMe[last_index];
if (*reverseMe != '') {
reverseMe[last_index] = '';  // shorten the inner string by one
// recurse on the shorter string
ReverseStringRecursive(reverseMe + 1);
// save the outer two characters
reverseMe[last_index] = *reverseMe;
*reverseMe = save_char;
}
}

您正在覆盖终止'',从而损坏您的字符串。当您的 while 循环存在时,reverseMe[position]位于''处,然后用值holder覆盖它。 您的字符串不再以 null 结尾,并且在下一个 while 循环中,当它访问数组边界之外时,您将在下一个 while 循环中获得未定义的行为。