2020-09-20

7074 ワード

jsは再帰的にs=a+a+aa+aa+++a++を実現します.+a...aの値で、その中のaは1つの数字です.例えば、2+22+22+2222
二つの再帰、一つの実現はそれぞれの項目を求め、一つは積算を実現するという原理は同じです.
 //      m     ,n  a  
        function qiu(m, n) {
         //           
            if (m == 1) {
        //        ,     n   
                return n
            } else {
     
                return qiu((m - 1), n) + n * Math.pow(10, (m - 1));
            }
        }
        console.log(qiu(1, 2))  //2     ==>2 10 0  
        console.log(qiu(2, 2))  //22    ==>   (   2) 2(  n)  10 1  ,  22
        console.log(qiu(3, 2))  //222   ==>   (   22) 2(  n)  10 2  ,  222
        console.log(qiu(4, 2))  //2222  ==>   (   222) 2(  n)  10 3  ,  2222
        console.log(qiu(5, 2))  //22222 ==>   (   22) 2(  n)  10 4  ,    22222


        function qiuHe(x, y) {
       //        ,x      ,y  a  
            if (x == 1) {
     
                return qiu((1), y)
            } else {
     
                return qiu(x, y) + qiuHe((x - 1), y)
            }
        }
        console.log(qiuHe(2, 2))    //24
        console.log(qiuHe(3, 2))    //246