如果它总是返回相同的参数值,它仍被视为递归算法吗?

时间:2018-02-19 10:39:51

标签: algorithm recursion

例如,对于:

BEGIN Algorithm(n)
    IF (n EQUALS 0) THEN
        RETURN 1
    ELSE
        RETURN Algorithm(n+5)/(n+5)
    END IF
END
无论RETURN Algorithm(n+5)/(n+5)的参数值是什么,

RETURN Algorithm(1)始终为n。这仍然被认为是一种递归算法吗?

2 个答案:

答案 0 :(得分:2)

如果n = -5怎么办?你可以以division / 0错误结束! 这意味着算法结果不会始终为1。

答案 1 :(得分:0)

什么决定一个函数是否递归是有可能它会自称无论结果多么愚蠢

describe('The menu', function () {
     /* TODO: Write a test that ensures the menu changes
      * visibility when the menu icon is clicked. This test
      * should have two expectations: does the menu display when
      * clicked and does it hide when clicked again.
      */

      it('the menu changes visibility when the menu icon is clicked', function () {

          var menuIconClicked, menuChangesWhenClicked = false, 
          menuChangesWhenClickedAgain = false;

          $(".menu-icon-link").click(function(){
            var $this = $(this);
            $(this).data('clicked', true);
            if($(this).data('clicked')) {
                menuIconClicked=true // if menu icon is clicked, set the menuIconClicked value to true
                 if (menuIconClicked && $('body').hasClass(null)) {
                    menuChangesWhenClicked=true;

          }
            }
            if($this.data('clicked') && menuIconClicked===true) { 
                menuIconClicked=false // if menu icon is clicked when it already has been clicked aka menuIconClicked===true
                if (!menuIconClicked && $('body').hasClass('menu-hidden')) {
                    menuChangesWhenClickedAgain=true;

          }
            }

        });
            expect(menuChangesWhenClicked).toBe(true);

            expect(menuChangesWhenClickedAgain).toBe(true);

      });

});

这会返回BEGIN Algorithm(n) IF (n EQUALS 0) THEN RETURN 1 ELSE RETURN Algorithm(n - 1) END IF END 的零和所有正参数。对于否定,它可能会溢出。 Abyway每次递归都是死代码,因为它可能是:

1