recursion

Recursive to Iterative Pascal's Triangle

谁说我不能喝 提交于 2020-01-11 13:39:34
问题 I wonder how to convert a recursive function/class to an iterative one. I have made a recursive Pascal's triangle, and now need to compare it to an iterative. public class RecursivePascal extends ErrorPascal implements Pascal { private int n; RecursivePascal(int n) throws Exception { super(n); this.n = n; } public void printPascal() { printPascal(n, false); } public void printPascal(boolean upsideDown) { printPascal(n, upsideDown); } private void printPascal(int n, boolean upsideDown) { if (n

Recursive to Iterative Pascal's Triangle

陌路散爱 提交于 2020-01-11 13:39:10
问题 I wonder how to convert a recursive function/class to an iterative one. I have made a recursive Pascal's triangle, and now need to compare it to an iterative. public class RecursivePascal extends ErrorPascal implements Pascal { private int n; RecursivePascal(int n) throws Exception { super(n); this.n = n; } public void printPascal() { printPascal(n, false); } public void printPascal(boolean upsideDown) { printPascal(n, upsideDown); } private void printPascal(int n, boolean upsideDown) { if (n

recursively find subsets

谁都会走 提交于 2020-01-11 13:24:14
问题 Here is a recursive function that I'm trying to create that finds all the subsets passed in an STL set. the two params are an STL set to search for subjects, and a number i >= 0 which specifies how big the subsets should be. If the integer is bigger then the set, return empty subset I don't think I'm doing this correctly. Sometimes it's right, sometimes its not. The stl set gets passed in fine. list<set<int> > findSub(set<int>& inset, int i) { list<set<int> > the_list; list<set<int> >:

How to trace a recursion?

走远了吗. 提交于 2020-01-11 10:26:07
问题 I saw this code online but what I am asking is how did the program come up with an answer of 12 ? I did a tracing of the program, and I only come up with an answer of 6 . Why is the answer 12 ? The inputs are a=6 and b=6 . This is the code: public static int addxy(int a, int b) { if (a==0) return b; else if (b==0) return a; else return 1 + addxy(a, b-1); } 回答1: addxy(6,6) 1+addxy(6,5) 1+1+addxy(6,4) 1+1+1+addxy(6,3) 1+1+1+1addxy(6,2) 1+1+1+1+1+addxy(6,1) 1+1+1+1+1+1+addxy(6,0) = 12 回答2: Try

Convert hierarchical list to a flat list in java

徘徊边缘 提交于 2020-01-11 09:39:31
问题 I have a hierarchical list like below and I want to convert it to a flat list . I have wrote a method called convertToFlatList and have used it. But some elements are missing in the final results. What did i do wrong? Also is there a better way than the way I have used to convert my list to a flat list? I have added a sample code and something similar to the Objects I have to used in my scenario. Final result should be 1, 2, 3, 4, 5, 6, 7 import java.util.ArrayList; import java.util.Arrays;

PHP recursive delete function

半世苍凉 提交于 2020-01-11 09:01:30
问题 I wrote recursive PHP function for folder deletion. I wonder, how do I modify this function to delete all files and folders in webhosting, excluding given array of files and folder names (for ex. cgi-bin, .htaccess)? BTW to use this function to totally remove a directory calling like this recursive_remove_directory('path/to/directory/to/delete'); to use this function to empty a directory calling like this: recursive_remove_directory('path/to/full_directory',TRUE); Now the function is function

A recursive algorithm to find two integers in an array that sums to a given integer

[亡魂溺海] 提交于 2020-01-11 07:49:09
问题 I need an algorithm to determine if an array contains two elements that sum to a given integer. The array is sorted. The algorithm should be recursive and runs in O(n). The recursive step should be based on the sum, meaning the method passes the sum and return true or false depending on the end result (if two elements are found - return true, else - return false) Only linear data structures can be used. Any ideas are appreciated.. 回答1: You can convert any iterative algorithm into a recursive

Backtracking bruteforce Java password cracker

血红的双手。 提交于 2020-01-11 07:39:09
问题 I have this homework assignment to make a recursive method to crack a password of a given length, n (unlimited and unknown!) made of small English letters, a-z ONLY. Here's the class "Password" that creates a random password: import java.util.Random; public class Password { private String _password = ""; public Password(int length) { Random generator = new Random(); for (int i = 0; i < length; ++i) { this._password = this._password + (char) (generator.nextInt(26) + 97); } } public boolean

JavaScript Recursion to format lists from XML to HTML

大城市里の小女人 提交于 2020-01-11 07:27:08
问题 I have an exercise where it calls upon me to use recursion to output XML data into HTML list tags. Shamefully admitting my shortcomings in mathematics, I would like someone to show me how to implement recursive logic to XML's 'node structure' using JavaScript. Here is the outcome: JSFiddle EDIT Added sample XML to round off this topic and deleted unneeded code. The XML used to create the recursive function: <ddm> <menu0 submenu="true"><name>Welcome</name> <menu1>Home Page</menu1> <menu1

Recursive power function: approach

寵の児 提交于 2020-01-11 07:02:23
问题 I'm programming for a while now(beginner), and recursive functions are a somewhat abstract concept for me. I would not say I'm stuck, program works fine, I'm just wondering if the function itself could be written without the pow function in the code (but still doing exactly what the problem suggests) Problem: http://prntscr.com/30hxg9 My solution: #include<stdio.h> #include<math.h> int power(int, int); int main(void) { int x, n; printf("Enter a number and power you wish to raise it to: ");