Java中的几个算法分析测试

1.基本函数机器测试体会:

测试代码:

/*
 * FunctionValues.java
 *
 *  Created on: 2020年3月3日
 *      Author: Sdjzu_Nxy
 */
package homework;

public class FunctionValues {

	public static void main(String[] args) {
		int[] number = new int[] {1,2,4,8,16,32};
		for (int i = 0; i < number.length; i++) {
			output(number[i]);
		}
	}
	
	public static int fconstant(int number) {
		return 1;
	}
	
	public static double flogn(int number) {
		double value = Math.log(number) / Math.log(2);
		return value;
	}
	
	public static int fn(int number) {
		return number;
	}
	
	public static double fnlogn(int number) {
		return number*flogn(number);
	}
	
	public static int fnsquare(int number) {
		return number*number;
	}
	
	public static int fncube(int number) {
		return number*fnsquare(number);
	}
	
	public static double fpowern(int number) {
		return Math.pow(2,number);
	}
	
	public static double factorial(int number) {
		double value = 1;
		for(int i = 2;i <= number;i++) {
			value *= i;
		}
		return value;
	}
	
	public static void output(int number) {
		System.out.println("function is 1,value is:"+fconstant(number));
		System.out.println("function is log2(n),value is:"+flogn(number));
		System.out.println("function is n,value is:"+fn(number));
		System.out.println("function is nlog2(n),value is:"+fnlogn(number));
		System.out.println("function is n^2,value is:"+fnsquare(number));
		System.out.println("function is n^3,value is:"+fncube(number));
		System.out.println("function is 2^n,value is:"+fpowern(number));
		System.out.println("function is n!,value is:"+factorial(number));
		System.out.println();
	}
}

测试结果:
在这里插入图片描述

2.折半思想,测试机器跑递归的阶乘程序,当n=?,抛出异常:java.lang.StackOverflowError

/*

 * MaxFactorial.java
 *
 *  Created on: 2020年3月9日
 *      Author: Sdjzu_Nxy
 */
package homework;

import java.util.Scanner;

public class  MaxFactorial{
  
	public static void main(String[] args) {
		Factori aFactori = new Factori();
		Scanner input = new Scanner(System.in);
		int number = input.nextInt();
		System.out.print(aFactori.factorial(number));//方法1
		//System.out.print(factorial(number));方法2
		input.close();
	}

	static class Factori{
		public Factori() {}
		public double factorial(int number) {
			if(number == 1) {
				return 1;
			}
			else {
				return number*factorial(number - 1);
			}
		}
	}
	
	public static double factorial(int number) {
		if(number == 1) {
			return 1;
		}
		else {
			return number*factorial(number - 1);
		}
	}
}

1.方法1,创建内部类创建内部类对象,通过调用内部类方法测试:
在这里插入图片描述
2.方法2,直接调用定义的静态方法测试:
在这里插入图片描述
**出现的问题:**在用第二种方式测试的时候出现了我不理解的问题:第二种方法得到的数据显示测试值为13365时,抛出了异常,测试13364时没有抛出异常,只是得到的值为Infinity,我为了再次确定结论是否是正确的,我又输入13365运行了一遍,此时却没有抛出异常,于是又测试了第一种方法,发现第一种方法测试极值也是在跳动。通过查阅资料以及老师解释,线程请求的栈深度大于虚拟机允许的最大深度就是栈溢出即StackOverflowError。目前对于为什么极值出现跳动这个问题我还不太清楚没找到很好的解释。

3.请给出++x的执行次数与n函数关系(严格,不用O):

static int fun(int n) {
	int x = 1;
	for(int i = 0; i < n; i++){
		for(int j = 0; j < i; ++j){
			for(int k = 0; k < j; ++k){
				++x;
			}
		}
	}    
	return x;
}

分析:
1.我们先从内层循环考虑,如果内部++x执行一次,则j=1,上层i=2,最外层n=3,也就是说,如果给定外层n次,在保证++x能够被执行的情况下即n>=3,则内部两层循环的次数为:1+(1+2)+(1+2+3)+…+(1+2+3+…+n-1+n),接下来就是通过数学方法来计算这个这个式子的结果;
2.我们分别列一下:条件(n>=3)
1 n = 3
1+2 n = 4
1+2+3 n = 5
… …
1+2+3+…+n-3 n = n-3
1+2+3+…+n-3+n-2 n = n-2
根据数列求和我们先计算通项公式:(n-1)(n-2)/2 计算结果为:1/2(n^2 - 3n + 2)
我们在对上面所有项进行求和:1/2( (3^2 + 4^2 + 5^2 + … + n^2) + (-3n - 5)(n-2)/2 )
根据平方数列求和公式 1^2 + 2^2 +…+ n^2 = n(n+1)(2n+1)/6 则上式结果为
1/2*( n(n+1)(2n+1)/6 - 1^2 - 2^2 + (-3n^2 + n + 10)/2)
=1/2
( 1/6*( 2n^3 + 3n^2 + n - 30 + 3(-3n^2 + n + 10) ) )
=1/2*( 1/6*( 2n^3 - 6n^2 + 4n )
=1/6*( n^3 - 3n^2 + 2n )
=n(n-1)(n-2)/6

  • 3
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值