Lua solves the problem of going up the ladder (output specific steps, iterations, and Recursion according to the specified format)

Source: Internet
Author: User
Problem description

The stairs have n steps. You can go upstairs to level 1 or Level 2. Compile a program to list each step.

Thought comments are all in the Code for the first time. Lua is very good at script language source code.

-- [[Replicate table: Deep copy --] -------------------------------------------------------------------------- function table. copy (t) Local T2 ={}; for K, V in pairs (t) doif type (v) = "table" thent2 [k] = table. copy (V); elset2 [k] = V; endendreturn T2; end -- [[-------------------------------------------------------------------- format: Convert the table into a string for formatting output --]] -------------------------------------------------------------------------- Function table. formatout (T, h) Local out = ""; for K, V in pairs (t) doif type (V) = "table" thenif K = 1 thenfor I = 1, h do out = string. format ("% S % s", out, ""); endout = string. format ("% S % s", out, "{\ n"); endout = string. format ("% S % s", out, table. formatout (v, H + 1); If K = table. getn (t) thenfor I = 1, h do out = string. form At ("% S % s", out, ""); endout = string. format ("% S % s", out, "}, \ n"); endelseif K = 1 thenfor I = 1, h do out = string. format ("% S % s", out, ""); endout = string. format ("% S % s", out, "{"); endout = string. format ("% S % d,", out, V); If (k = table. getn (t) thenout = string. format ("% S % s", out, "}, \ n"); endendendreturn out; end -- [[------------------------------------------------------------------------ format output --]-] ------------------------------------------------------------------------- Function table. print (t) Local out = table. formatout (T, 0); -- remove the last ", \ n" two characters print (string. sub (Out, 1, String. len (out)-2); end -- [Response @ paramsolution_new @ format {1, 2} {2, 1} {1} {1, 1, solution @ format {} {, 1 }}@ meaning under a specific n value, all solutions, each element It is a value to be inserted to solve the value @ format integer @ meaning, such as the input status of 1 2 @ exmple: solution_new ={} solution ={{ 1} {2, 1} {1, 1, 1} value = 1 after running the function: solution_new = {1, 1} {2, 1} {1, 1} {1, 1} --] processing function update_solution (solution_new, solution, value) Local STR = string. format ("% d", value) -- convert the value into a character table. foreach (solution, function (idx, ELE) table. insert (Ele, STR) Table. insert (solution_new, ELE) End) end -- [[solution: All feasible solutions of level n step {{},{}}]] -- function f_digui (N) local Solution = {} if n = 1 thensolution = {1} elseif n = 2 thensolution = {1, 1 }, {2} elsesolution ={} update_solution (solution, f_digui (n-2), 2); update_solution (solution, f_digui (n-1), 1); endreturn solutionend -- [[solution: all feasible solutions of steps 1 to n are {{},{},{}},{},{},{}}} solution [I]: all feasible solutions of level I step {{},{ }, {}} From here, we can see that iteration requires more space to save data] -- function f_diedai (n) Local Solution ={} for I _n = 1, N doif I _n = 1 thensolution [I _n] = {1} elseif I _n = 2 thensolution [I _n] = {1, 1 }, {2} elsesolution [I _n] = {} update_solution (solution [I _n], table. copy (solution [I _n-2]), 2); update_solution (solution [I _n], table. copy (solution [I _n-1]), 1); endendreturn solution [N] end -- [n returns the combination of each n value from starti to endi] -- function test (s Tarti, endi, FUC) for I = starti, endi, 1 dolocal array = {} print () print ("N value:", I) if FUC = "digui" thentable. print (f_digui (I) elsetable. print (f_diedai (I) endendend vertex test (3, 4, "digui"); -- Test (1, 4, "diedai"); -- table. print ({1, 2}, {3, 4}, {1, 2}, {3, 4}); out = "Hello Lua! \ N this is the first Lua code to solve the problem of going up the stairs \ n there are several good places: \ N1, recursive and iterative conversions \ N2, table replication and table output \ N3, use of some basic syntax "; print (out); test (4, 4, "diedai"); dimensions [[all variables are local variables by default, and local variables must be used] ---- [the function parameter in Lua is "Reference", that is: changing the value of this parameter in a function will change the variable] ---- [[(Code normalization -- as a reference) generally, all functions in programming languages should not return meaningful values. expected results should be saved through parameters. For example, in the update_solution function, we need to obtain solution_new as a parameter, instead of returning in the form of return solution_new, it is generally an error code used for Error Control, indicating whether the function is normally executed. If you need to return a meaningful value, the function name should be named get _ *** or, if no meaningful value is returned, the function is hard to understand, or the value can be returned, for example, in the f_digui of the recursive function] --


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.