Recursive algorithm Java Implementation __ Coding

Source: Internet
Author: User

Originated from the NetEase algorithm pen test questions, before the recursive algorithm has a preliminary impression. Feeling recursion seems to be an iterative process. The original problem is decomposed into a number of problems similar to the original problem, until the small problem is known, that is, to find the exit of the recursive algorithm, then the problem back to the results of the solution. Don't say much nonsense, first on the question.

The title is this: a monkey picked a lot of peaches the first day, feel very happy immediately ate half of the peach total, and then feel not full and ate 3. The monkey felt that the peach would not immediately, so he set a rule: each time on the odd day to eat the total number of the remaining Peach 3 more, each time in even days to eat the total number of the remaining peach to eat more than half. Please enter a number of days so that the number of peaches remaining in the number of days is exactly 1, please print out how many peaches the monkey picked on the first day.

Problem decomposition: Set input number of days to N;

First, find the problem entrance: The 1th day, the number of remaining peach: F (1);

Secondly, we find the recursive equation of the problem, in which there are two recursive equations,

If n days are odd days: there is f (n) = (f (n+1) +3) *2

If n days are even days: f (n) = (f (n+1) +1) *2

Among them, odd days and even days of reciprocal recursion.

Finally, the problem ends: when the program asks for the Nth Day, F (n) = 1 is known.

First extract the recursive type

It's obviously recursive, where the day variable is used to hold the number of days you enter, for the recursive end of the flag. In recursion, the result of recursive completion is first judged, and then the recursive type is written.

      a recursive
		if (count = = day) of private static int sumoddday (int day, int count) {//Odd days return 1;
		else return 2* (Sumevenday (day, count+1) + 3);
		
	}

private static int Sumevenday (int day, int count) {    //even days of recursive
		if (count = = day) return 1;
		else return 2* (Sumoddday (day,count+1) + 1);
	}

Whether the total number of days is odd or even

private static int sumnum (int day, int count) {
			if (count%2 = 0) {return
				sumevenday (day, count);	   Even days
			}else {return
				sumoddday (day, count);       Odd days
			}
	}

Finally get the complete program code, as follows
Package Test;

Import Java.util.Scanner;

public class Test {public
	static void Main (String [] args) {
		System.out.println ("Please enter Days:");
		Scanner Scanner = new Scanner (system.in);
		int day = Scanner.nextint ();   Input value
		
		System.out.println (Sumnum (day,1));
	}
	
	private static int sumnum (int day, int count) {
			if (count%2 = 0) {return
				sumevenday (day, count);	   Even days
			}else {return
				sumoddday (day, count);       Odd days
			}
	}

	private static int sumoddday (int day, int count) {
		if (count = = day) return 1;
		else return 2* (Sumevenday (day, count+1) + 3);	
	}

	private static int Sumevenday (int day, int count) {
		if (count = = day) return 1;
		else return 2* (Sumoddday (day,count+1) + 1);
	}



As we all know, the recursive algorithm needs a lot of memory space, if the subject limits the running memory, then the recursive algorithm is not very applicable. So, the Internet to find about how to convert the recursive algorithm to the cycle of methods, summed up the following first.

1. Use stack

2. Use cycle

The most of the college class should be the recursive algorithm of the tree and the conversion of the non-recursive algorithm. Landlord also not to say, online or textbooks have code.


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.