recursion

fibonacci series - recursive summation

风格不统一 提交于 2020-01-12 05:35:28
问题 Ok, I initially wrote a simple code to return the Fibonacci number from the series based on the user input.. n=5 will produce 3.. static int fibonacci(int n) { if (n == 1) return 0; else if (n == 2) return 1; else return (fibonacci(n - 1) + fibonacci(n - 2)); } I was thinking of modifying the code to return the sum of the series rather than just returning the value from the series and while trying to do the sum I accidentally added 1 to the return statement and to my surprise, it returned the

Java: how to get all subdirs recursively?

江枫思渺然 提交于 2020-01-12 05:32:05
问题 Before debugging the late-hour-out-of-bound-recursive-function: is there a command to get subdirs? giveMeSubDirs(downToPath) ? // WARNING: RECURSION out of bound or too much data public HashSet<FileObject> getAllDirs(String path) { HashSet<FileObject> checkedDirs = new HashSet<FileObject>(); HashSet<FileObject> allDirs = new HashSet<FileObject>(); String startingPath = path; File fileThing = new File(path); FileObject fileObject = new FileObject(fileThing); for (FileObject dir : getDirsInDir

How to create cartesian product over arbitrary groups of numbers in Java?

纵然是瞬间 提交于 2020-01-12 05:08:27
问题 Let's say I have 2 groups of numbers: {1, 2, 3}, {4, 5} I'd like to create an algorithm (in Java) that outputs the following 6 combinations: 1,4 1,5 2,4 2,5 3,4 3,5 There can be an arbitrary number of groups and an arbitrary number of members within each group. So in the above example, there are 2 groups with the first group having 3 members and the second group having 2 members. Another example is the following (3 groups, 3 members in first groups and 2 members in second and third groups):

How to create cartesian product over arbitrary groups of numbers in Java?

依然范特西╮ 提交于 2020-01-12 05:08:06
问题 Let's say I have 2 groups of numbers: {1, 2, 3}, {4, 5} I'd like to create an algorithm (in Java) that outputs the following 6 combinations: 1,4 1,5 2,4 2,5 3,4 3,5 There can be an arbitrary number of groups and an arbitrary number of members within each group. So in the above example, there are 2 groups with the first group having 3 members and the second group having 2 members. Another example is the following (3 groups, 3 members in first groups and 2 members in second and third groups):

Get All Children to One List - Recursive C#

江枫思渺然 提交于 2020-01-12 04:30:30
问题 C# | .NET 4.5 | Entity Framework 5 I have a class in Entity Framework that looks like this: public class Location { public long ID {get;set;} public long ParentID {get;set;} public List<Location> Children {get;set;} } ID is the identifier of the location, ParentID links it to a parent, and Children contains all of the children locations of the parent location. I'm looking for some easy way, likely recursively, to get all "Location" and their children to one single List containing the Location

Linq recursive parent child

谁说胖子不能爱 提交于 2020-01-12 03:53:09
问题 I have data from database like that : id | description | parent_id ----------------------------------------- 1 | Record 1 | null 2 | Record 2 | 1 3 | Record 3 | null 4 | Record 4 | 2 5 | Record 5 | 3 I want to process using Linq, and it should be result like this : Record 1 - Record 2 - Record 4 Record 3 - Record 5 I got solution : Recursive LINQ query: select item and all children with subchildren but unfortunately the result only has two level, because i want the result in multi level > 2

Can a backtracking tail recursive algorithm be converted to iteration?

六眼飞鱼酱① 提交于 2020-01-12 01:59:16
问题 Let's take the Knight Tour problem. Can that be converted to iteration? What is confunsing me is the backtracking part. How do I backtrack in a loop? Do I have to necessarily use a stack data-structure to implement backtracking when I go from recursion to iteration? I asked this question in a better way here: Can someone describe through code a practical example of backtracking with iteration instead of recursion? 回答1: No, it can't be. All recursive algorithms can be implemented iteratively,

Can you quickly tell me if this pseudocode makes sense or not?

北城余情 提交于 2020-01-11 14:45:07
问题 I believe my code is now foolproof. I will write up the pseudocode now. But I do have one question. Why does DRJava ask that I return something outside of my if statements? As you can see I wrote for ex: "return 1;" just because it asked. It will never return that value however. Can someone explain this to me? public class assignment1question2test { public static void main(String[] args) { int[] a = new int[50]; int l = 0; int r = a.length; for(int i=0; i<r; i++) { a[i] = 1; } a[0] = 10; for

Recursive function to check digits

杀马特。学长 韩版系。学妹 提交于 2020-01-11 13:57:31
问题 Write a recursive function to check how many digits in the number can be divided by the digit which is after them. Example: 84963 should return 2, because 8 can be divided by 4 and 6 can be divided by 3. My function doesnt seem to output anything at all. #include <iostream> using namespace std; int fun (int n); int main() { int n; cin >> n; cout << fun(n) << endl; return 0; } int fun(int n){ int count = 0; if (fun(n % 100) % fun(n % 10) == 0) count++; return count; } 回答1: Your recursion does

I am not able to understand the recursion of functions. How it works? how the values are stored and all?

╄→尐↘猪︶ㄣ 提交于 2020-01-11 13:49:13
问题 I am not able to understand the recursion of functions. How it works? how the values are stored and all? int tree_size(struct node* node) { if (node==NULL) { return(0); } else { return(tree_size(node->left) + tree_size(node->right) + 1); } } 回答1: When entering a function, a new stack frame is created (on the stack in memory). The stack frame keeps track of the local data within that function, such as locally defined variables and incoming arguments. (And other things such as return address