Java小练习题(一)

本文通过四个示例展示了Java中递归的使用,包括求圆面积、斐波那契数列、数列求解及阶乘计算。此外,还探讨了两种不同的数组元素操作方法,分别是数组元素除首项求新值和数组反转。这些实例深入浅出地解释了递归在编程中的应用以及数组操作的基本技巧。
摘要由CSDN通过智能技术生成

1、求圆的面积

public class Circel {
	// 半径
	double radius;

	/*
	 * 测试类
	 */
	public static void main(String[] args) {
		Circel circle = new Circel();
		circle.radius = 5;
		double area = circle.findArea();
		System.out.println("半径为" + circle.radius + "的圆的面积为:" + area);
	}

	/**
	 * @Description 返回圆的面积的方法
	 * @author zhenghuhu
	 * @date 2021-7-10 9:13:29
	 * @return
	 */
	public double findArea() {
		return Math.PI * radius * radius;
	}
}

2、斐波那契数列

public class Fibonacci {
	public static void main(String[] args) {
		Fibonacci f = new Fibonacci();
		System.out.println(f.fibonacciTest(7));
	}
	/**
	 * 求斐波那契数列某一位上的值
	* @Description 
	* @author zhenghuhu
	* @date 2021-7-10 11:23:17
	* @param n 所求的斐波那契数列的位数
	* @return  所求固定位置上的斐波那契数列的值
	 */
	public int fibonacciTest(int n) {
		if (n == 1) {
			return 1;
		}else if (n == 2) {
			return 1;
		}
		else {
			return fibonacciTest(n - 1) + fibonacciTest(n - 2);
		}
	}
}

3、函数的递归调用

3.1、已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n), 其中n是大于0的整数,求f(10)的值。

public class MethodRecursion {
	public static void main(String[] args) {
		MethodRecursion methodRecursion = new MethodRecursion();
		int fn = methodRecursion.f(10);
		System.out.println(fn);
	}
	/**
	 * 递归调用实现函数的求解
	* @Description 已知有一个数列:f(0) = 1,f(1) = 4,f(n+2)=2*f(n+1) + f(n),
	*    其中n是大于0的整数,求f(10)的值。
	* @author zhenghuhu
	* @date 2021-7-10 10:36:21
	* @param n
	* @return
	 */
	public int f(int n) {
		if (n == 0) {
			return 1;
		}else if(n == 1) {
			return 4;
		}else {
			return 2 * f(n -1 ) + f(n - 2);
		}
	}
}

3.2、已知一个数列:f(20) = 1,f(21) = 4,f(n+2) = 2*f(n+1)+f(n),其中n是大于0的整数,求f(10)的值。

public class MethodRecursion {
	public static void main(String[] args) {
		MethodRecursion methodRecursion = new MethodRecursion();
		int fn2 = methodRecursion.f2(10);
		System.out.println(fn2);
	}
	/**
	 * 
	* @Description 已知一个数列:f(20) = 1,f(21) = 4,f(n+2) = 2*f(n+1)+f(n),
	* 其中n是大于0的整数,求f(10)的值。
	* @author zhenghuhu
	* @date 2021-7-10 10:44:35
	* @param n
	* @return
	 */
	public int f2(int n) {
		if (n == 20) {
			return 1;
		}else if (n == 21) {
			return 4;
		}else {
			return f(n + 2) - 2 * f(n + 1);
		}
	}
}

3.3、连续求和

public class Recursion {
	public static void main(String[] args) {
		Recursion recursion = new Recursion();
		int sum = recursion.sum(100); // 求1~100的和
		System.out.println(sum);
	}
	/**
	 * 连续求和
	* @Description 
	* @author zhenghuhu
	* @date 2021-7-10 9:45:26
	* @param n
	* @return
	 */
	public int sum(int n) {
		if (n == 1) {
			return 1;
		}else {
			return n + sum(n - 1);
		}
	}
}

3.4、求阶乘

public class Recursion {
	public static void main(String[] args) {
		Recursion recursion = new Recursion();
		long product = recursion.product(10); // 求10的阶乘
		System.out.println(product);
	}
	/**
	 * 求阶乘
	* @Description 
	* @author zhenghuhu
	* @date 2021-7-10 9:48:06
	* @param n
	* @return
	 */
	public long product(int n) {
		if (n == 1) {
			return 1;
		}else {
			return n*product(n - 1);
		}
	}
}

4、println的方法的重载

/**
 *此题考察println的方法的重载
* @Description
* @author zhenghuhu
* @version
* @date 2021-7-7 20:55:32
*
 */
public class ArrayPrintTest {
	ublic static void main(String[] args) {
		int[] arr1 = new int[] {1, 2, 3, 4};
		System.out.println(arr1);
		System.out.println("arr1 = " + arr1);
		
		char[] arr2 = new char[] {'a', 'b', 'c'};
		System.out.println(arr2);
		System.out.println("arr2 = " + arr2);
	}
}

运行结果展示如下:

[I@15db9742
arr1 = [I@15db9742
abc
arr2 = [C@6d06d69c

5、数组换新

  • 方式一:
/*
 * 定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
 * 让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的
 * 新值。遍历新的数组。
 */
public class MicroSoftTest {
	public static void main(String[] args) {
		int[] arr = new int[]{12,3,3,34,56,77,432};
		System.out.println(Arrays.toString(arr));
		int temp = arr[0];
		for (int i = 0; i < arr.length; i++) {
			arr[i] = arr[i] / temp;
		}
		System.out.println(Arrays.toString(arr));
	}
}
  • 方式二:
/*
 * 定义一个int型的数组:int[] arr = new int[]{12,3,3,34,56,77,432};
 * 让数组的每个位置上的值去除以首位置的元素,得到的结果,作为该位置上的
 * 新值。遍历新的数组。
 */
public class MicroSoftTest {
	public static void main(String[] args) {
		int[] arr = new int[]{12,3,3,34,56,77,432};
		System.out.println(Arrays.toString(arr));
		for (int i = arr.length - 1; i >= 0 ; i--) {
			arr[i] = arr[i] / arr[0]; 
		}
		System.out.println(Arrays.toString(arr));
	}
}

6、数组元素的反转

  • 方式一:
public class ArrayReverse {
    public static void main(String[] args) {
		String[] strs = new String[] {"WANG", "DA", "CHUAI", "JIU", "SHI", "WANG", "LI"};
		for (int i = 0; i < strs.length; i++) {
			System.out.print(strs[i] + "\t");
		}
		// 实现反转
		for (int i = 0; i < strs.length / 2; i++) {
			String temp = strs[i];
			strs[i] = strs[strs.length - i -1];
			strs[strs.length - i -1] = temp;
		}
		System.out.println("\n反转之后:");
		for (int i = 0; i < strs.length; i++) {
			System.out.print(strs[i] + "\t");
		}
	}
}
  • 方式二:
public class ArrayReverse {
    public static void main(String[] args) {
		String[] strs = new String[] {"WANG", "DA", "CHUAI", "JIU", "SHI", "WANG", "LI"};
		for (int i = 0; i < strs.length; i++) {
			System.out.print(strs[i] + "\t");
		}
	    // 实现反转
	    for (int i = 0, j = strs.length - 1; i < j; i++, j--) {
			String temp = strs[i];
			strs[i] = strs[j];
			strs[j] = temp;
		}
		System.out.println("\n反转之后:");
		for (int i = 0; i < strs.length; i++) {
			System.out.print(strs[i] + "\t");
		}
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值