Hello. I learn by myself programming, but I've noticed a huge problem with the tasks recursion is slightly heavier than the base.
For example the second section with the objectives of the recursion on this website:
codingbat.com/java/Recursion-2The entire section is based on one method(
codingbat.com/prob/p145416 — show solution), the perception of which is my problem. I knew it — drew the principle of its operation, but still difficult, I have no sense of perception, logic; and say if you slightly change the condition(the next task), I can't figure out what needs to change in order to apply this method...
I decided to save this for later, and take care of lighter tasks, one of which I invented myself while watching this video(not the formula):
https://youtu.be/UfEiJJGv4CE?t=168Objective: given a natural number N. Find the number of integers in the interval [0-N] containing a figure "3" in the loops by using recursion.
And I solved it... but my decision not like it. I am passing two parameters - Number and Сurrent which is exactly the same and are just buffers for the values used in the code part which looks in the current number "3" and part of the code which walks backward from N to 0.
public class ThreeCounter { public static int ThreeCounter(int number, int current) { //Base case -- the algorithm went through all the numbers if(current == 0) { return 0; } //Base case? the second recursion -- algorithm went through all the numbers, the current number of if (number == 0) { ThreeCounter return(current-1, current - 1); } if(number % 10 == 3) { return 1 + ThreeCounter(current-1, current - 1); } ThreeCounter return(number/10, current); } public static void main(String[] args) { int n = 100; System.out.println(ThreeCounter(n, n)); } }
How to make the code "more correct" because I don't think it would be reasonable to write: "Solkatronic(taraseviciene, tozhesamoe number)"? What would you suggest in order to understand the above algorithm? There and so all is explained at the elementary level... to Beat my head until I understand?