0

使用以下代码,我将添加一个递归函数,而无需更改代码中的任何内容以使其工作:

 sf::RenderWindow window(sf::VideoMode(WIDTH, HEIGHT), TITLE);
 int stop = 800;
 sf::Color colour1(sf::Color::Black), colour2(sf::Color::Red);

 // Start the main loop
 while (window.isOpen()) {          
     // Process events
     sf::Event event;

     while (window.pollEvent(event)) {
         // check the type of the event...
         switch (event.type) {
             // window closed
             case sf::Event::Closed:
                 window.close();
                 break;

             case sf::Event::MouseMoved: //mouse moved
                 stop = mapXtoMinSize(event.mouseMove.x); // convert x coordinate of mouse to minimum size of square to draw
                 break;    
             // we don't process other types of events
             default:
                break;
          } //end switch     
      } //End-Process events
      window.clear(colour2);
 }

我只是想知道如何去做。

4

1 回答 1

0

递归函数通常是这样的:

int recursive(int x) {
    if (x == 0) {
        return 0;
    } else {
        return (x + recursive(x - 1));
    }
}

这将对从 0 到 x 的所有整数求和,通过重复调用自身来计算总和(x - 1)并将其添加到结果中。如果x == 0遇到基本情况 , ,则递归结束并且调用展开,直到它们到达原始情况,此时已计算总和。请更具体。

在不知道你试图用你的递归函数实现什么的情况下,我无法给你更多建议。我不知道您评论“不更改代码中的任何内容”是什么意思。

于 2013-11-08T15:02:50.290 回答