Java Basics--"Exercise Two" function _ Block Chain

Source: Internet
Author: User
Tags gcd
"Exercise two" function

1. Define a method that can determine and return the maximum value of two integers, and call its own method to test whether it is correct.

Package T2;
public class maxmethod{public
	static void Main (String [] args) {
	System.out.println (max (10,9));//Call Max function
	public
	static int max (int a,int b) {//defines the maximum value int max that a function can judge and return two integers
		;
		if (a>b) {max=a;}
		else {max=b;}
		return max;
	}




2, Hanoi Tower problem: Hanoi is based on a legend formed a problem. Hanoi (also known as Heneta) is a puzzle toy from an ancient Indian legend. When the great Brahma created the world, he made three diamond pillars and stacked 64 gold discs on a pillar from the bottom up to the top in order of size. Brahma ordered the Brahman to rearrange the disc from below to another pillar in order of size. It is also stipulated that the disc cannot be enlarged on a small disc and only one disc can be moved between the three pillars at a time. Take 5 pieces of gold discs for example.

Package T2;
public class hanoi{public
	static void main (String []args) {
		Hanoi (' A ', ' B ', ' C ', 5);
	}
	public static void Hanoi (char A,char b,char c,int N) {
		//define a method pillar A, pillar B, Pillar C,n disc
		//a pillar moved to pillar C, column B as intermediate temporary storage
		if ( n==1) {
			System.out.println ("move from" + "a" + "to" + "C");
		} else{
			Hanoi (a,c,b,n-1);
			System.out.println ("move from" + "a" + "to" + "B");
			Move N-1 discs from a to b,c for intermediate temporary storage
			System.out.println ("moves from" + "a" + "to" + "C");
			Move the largest disc from A to C
			Hanoi (b,a,c,n-1);
			System.out.println ("move from" + "a" + "to" + "B");
			Move N-1 discs from B to c,a as intermediate temporary storage
		}}


3, write a method for the gcd of any two positive integers, call this method to find 16 and 24 gcd.

Note: For gcd use of the Euclidean method, the ancient Chinese mathematician Qinjiushao in the book nine in 1247, recorded in this method, the process is as follows:

(1) two numbers m and N provided

(2) the remainder R (r=m%n) is obtained with n except M

(3) To determine whether R is 0, if r=0, at this time the N value is GCD, the calculation ends. If r≠0, update dividend and divisor, n send m (ie m=n), R send N (ie n=r), go to (2).

r=m%n;

while (r!=0) {m=n;n=r;r=m%n}

Package T2;
Import Java.util.Scanner;
public class maxgys{public
	static void main (String []args) {
		Scanner sc=new Scanner (system.in);
		System.out.print ("Please enter first number:");
		int N=sc.nextint ();
		System.out.print ("Please enter a second number:");
		int M=sc.nextint ();
		Maxgys (n,m);
	
	}
	public static void Maxgys (int n,int m) {
		int r=n%m;
		if (r==0) {
			System.out.println (m+) is GCD. ");
		} else{
			n=m;
			M=r;
			Maxgys (n,m);
		}
	
	/*
         public static int Maxfactor (int m,int n) {
		int r=m%n;
		while (r!=0) {
			m=n;
			N=r;
			r=m%n;
		}
		return n;
	}
	
	public static void Main (string[] args) {
		int f=maxfactor ();
		System.out.println (f);
	}
         */
}

4. Write a recursive method getpower (int x,int y) to compute the Y-power of X (assuming that x,y are all positive integers) (Do not use Math.pow ()) and call it in main Main method for a power of 2 10 times.

Package T2;
public class power{public
	static void main (String []args) {
		System.out.println (Getpower (2,10));
	}
	public static int getpower (int x,int y) {
		
		/* IF (y==1) {return
			x;
		} else{return
			x*getpower (x, y-1);
		}
		*
		/if (y==0) {return
			1;
		} else{return
			x*getpower (x,y-1);
		}
	
	}


5. The Fibonacci series: the Fibonacci sequence (Fibonacci sequence), also known as the Golden Section series, was introduced by the mathematician Leonardo's Fibonacci (Leonardoda Fibonacci) as an example of rabbit breeding, which is also known as the "Rabbit series", Refers to such a series: 1, 1, 2, 3, 5, 8, 13, 21, 、...... Mathematically, the Fibonacci sequence is defined as the following recursive method: F (0) =1,f (1) =1, f (n) =f (n-1) +f (n-2) (n>=2,n∈n*). Recursive implementation algorithm of Fibonacci series;

Package T2;
public class  FIBONACCI{//1, 1, 2, 3, 5, 8, 13, 21, 、......
	public static void Main (String []args) {
		System.out.println (Fibonacci (3));
	}
	public static int Fibonacci (int n) {
		if (n==0| | N==1) {return
			1;
		} else if (n>=2) {return
			Fibonacci (n-1) +fibonacci (n-2);
		}
		return Fibonacci (n);
	}

}

6, do not use the relevant methods provided by the math class, write a method of the public static int round (double n) can be rounded to decimal N to return an integer. Take note of n plus or minus. Use some positive or negative decimals to test whether you have written the correct method. (Basic idea: n is a decimal, then (int) n is the integer part of N, N minus its own integer part, by comparing the size of positive or negative 0.5, you can decide how many integers after rounding)

Package T2;
Import Java.util.Scanner;
public class Method {public
	static void Main (string[] args) {
		Scanner sc=new Scanner (system.in);
		System.out.print ("Please enter a decimal number (rounded integer):");
		Double n=sc.nextdouble ();
		The result of the rounding of the System.out.println (n+) is: "+round (n));
	}

	public static int round (double n) {
		int p= (int) n;
		if (n>0) {			
			if (n-p>=0.5) {return
				p+1;
			} else{return
				P
			}
		} else{
			if (n-p<=-0.5) {return
				p-1;
			} else{return
				p;}}}
	


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.