在c ++

时间:2019-03-06 22:15:30

标签: c++ recursion

使用main函数,相对简单地分配数组ReverseStringRecursive中的数组。但是限制是我只能使用一个int和一个char声明变量的其他东西(这包括禁止for循环等)。另外,不得使用额外的库,我仅限于iostreamconio.h。我遇到的问题是string会先打印然后在我只需要向后打印时向后打印。 reverseMe变量指向string中包含main的{​​{1}}。此功能不是假设只打印"abcdefghijklmnopqrstuvwxyz",然后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] == '\0') {
    return 1;
}
else {
    holder = reverseMe[position];
}

ReverseStringRecursive(reverseMe + 1);

while (reverseMe[position] != '\0') {
    position++;
}
reverseMe[position] = holder;

return position;
}

我应该得到什么:

"abcdefghijklmnopqrstuvwxyz zyxwvutsrqponmlkjihgfedcba"

2 个答案:

答案 0 :(得分:2)

难题。您必须在每次递归中缩短内部字符串,方法是在调用递归函数之前在最后一个字符上放置'\ 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)

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

以下代码应在Windows系统上运行。要使其在Linux上运行,只需注释掉conio.h include行,注释__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] != '\0')
    last_index++;

  char save_char = reverseMe[last_index];

  if (*reverseMe != '\0') {
    reverseMe[last_index] = '\0';  // 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;
  }
}

答案 1 :(得分:0)

您正在覆盖终止的return Card( child: IntrinsicHeight( child:Row( mainAxisSize: MainAxisSize.min, crossAxisAlignment: CrossAxisAlignment.stretch, children: <Widget>[ CachedNetworkImage( height: 80, imageUrl: posterUrl, placeholder: (context, url) => new CircularProgressIndicator(), errorWidget: (context, url, error) => new Icon(Icons.error), ), Column( crossAxisAlignment: CrossAxisAlignment.start, children: <Widget>[ Text(_show['name']), Expanded(child: Text('First Episode: ' + _show['first_air_date'])), ], ), ], ), ), ); ,因此损坏了字符串。当while循环存在时,'\0'位于reverseMe[position],然后用值'\0'覆盖它。您的字符串不再以null结尾,并且在下一个while循环中访问字符串数组之外时,您将获得未定义的行为。