Java Exercise 12.1

Java Exercise 12.1

Recursively and cyclically are used to program:

1. Enter an integer n, ask for n!

method 1:

package com.shangjiti.aoian;
import java.util.Scanner;
public class No1 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("请输入一个整数:");
		Scanner sc=new Scanner(System.in);
		int sum=1;
		int n=sc.nextInt();
		for(int i=1;i<=n;i++)
			sum*=i;
		System.out.println(sum);
	}
}

Method 2:

package com.shangjiti.aoian;/*输入一个整数n,求n!*/
import java.util.Scanner;
public class No1_1 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("请输入一个整数:");
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		System.out.println(n+"的阶乘是"+jieCheng(n));
	}
	public static int jieCheng(int n) {
    
    
		if(n==1) {
    
    
			return 1;
		}
		else {
    
    
			return n*jieCheng(n-1);
		}
	}
}

2. Find the nth term of Fibonacci sequence 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144,…

method 1:

package com.shangjiti.aoian;
import java.util.Scanner;
public class No2 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("请输入一个整数:");
		Scanner sc=new Scanner(System.in);
		int n=0,n1=1,n2=1;
		int num=sc.nextInt();
		if(num<=2)
			System.out.println("1");
		else 
		{
    
    
			for(int i=3;i<=num;i++)
			{
    
    
				n=n1+n2;
				n1=n2;
				n2=n;				
			}
			System.out.println(n);
		}
	}
}

Method 2:

package com.shangjiti.aoian;
import java.util.Scanner;
public class No2_2 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("请输入一个整数:");
		Scanner sc=new Scanner(System.in);
		int n=sc.nextInt();
		System.out.print(fib(n));
	}
	public static int fib(int num) {
    
    
		if(num==1||num==2)
			return 1;
		else {
    
    
			return fib(num-1)+fib(num-2);
		}
	}
}

3. The problem of monkey eating peaches: the monkey picked off several peaches on the first day, and ate half of it immediately, but was not addicted, ate one more, and ate half of the remaining peaches the next morning, and ate more One. After that, every morning I ate half and one of the remaining half of the previous day. When I wanted to eat again in the morning of the 10th day, I saw that there was only one peach left. How many were picked on the first day

method 1:

package com.shangjiti.aoian;
public class No3 {
    
    
	public static void main(String[] args) {
    
    
		int day,s=1;
		for(day=9;day>0;day--)
		{
    
    
			s=2*(s+1);
		}
		System.out.println("第一天的桃子数是"+s);
	}
}

Method 2:

package com.shangjiti.aoian;
public class No3_3 {
    
    		//猴子吃桃问题
	public static void main(String[] args) {
    
    
		System.out.println("第一天的桃子数是"+peach(1));		
	}
	public static int peach(int day) {
    
    
		if(day==10)
			return 1;
		else
			return (peach(day+1)+1)*2;
	}
}

4.5 students sit together, the previous student is 2 years younger than the next student, the first student is 10 years old this year, then find the age of the fifth student

method 1:

package com.shangjiti.aoian;
public class No4 {
    
    			//求第5个学生的年龄
	public static void main(String[] args) {
    
    
		int n=10;
		for(int i=1;i<5;i++)
		{
    
    
			n=n+2;
		}
		System.out.println(n);
	}
}

Method 2:

package com.shangjiti.aoian;
public class No4_4 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println(age(5));
}
	public static int age(int n) {
    
    
		if(n==1)
		{
    
    
			return 10;
		}
		else 
		{
    
    
		 return age(n-1)+2;
		}
	}
}

5. Use recursion to achieve summation within 1 to 100

method 1:

package com.shangjiti.aoian;
public class No5 {
    
    		//1到100以内的求和
	public static void main(String[] args) {
    
    
		int sum=0;
		for(int i=1;i<=100;i++)
			sum+=i;
		System.out.println(sum);
	}
}

Method 2:

package com.shangjiti.aoian;
public class No5_5 {
    
    	//利用递归实现1到100以内的求和
	public static void main(String[] args) {
    
    
		System.out.println(sum(100));
	}
	public static int sum(int num){
    
    
		if(num == 1)
		{
    
    
			return 1;
		}
		else
		{
    
    
			return num + sum(num - 1);
		}
	}
}

6. A ball falls freely from a height of 100 meters, and bounces back to half of its original height each time it hits the ground. When it falls again, how many meters does it travel when it hits the ground for the 10th time? How high is the 10th rebound?

method 1:

package com.shangjiti.aoian;
public class No6 {
    
    		//小球落地问题
	public static void main(String[] args) {
    
    
		int i;
		double h=50,s=100;
		for(i=2;i<=10;i++)
		{
    
    
			s=s+2*h;
			h=h/2;
		}
		System.out.println("第十次反弹高度为:"+h);
		System.out.println("第10次落地共经过的米数:"+s);
	}
}

Method 2:

package com.shangjiti.aoian;
public class No6_6 {
    
    
	public static void main(String[] args) {
    
    
		System.out.println("第十次反弹高度为:"+h(10));
		System.out.println("第10次落地共经过的米数:"+s(10));
	}
	public static double h(int n) {
    
    
		if(n==1)
			return 50;
		else
			return h(n-1)/2;
	}
	public static double s(int n) {
    
    
		if(n==1)
			return 100;
		else
			return s(n-1)+h(n-1)*2;
	}
}

Guess you like

Origin blog.csdn.net/m0_46653702/article/details/109443930