作业1:算法分析

1、编写程序计算n = 1、2、4、8、16、32时,以下函数的值:

1、logn、n、nlogn、n2、n3、2n、n!

用excel做表,保存为PDF。提交"漂亮的"表格(log以2为底)

package homework.demo01;

import java.util.ArrayList;
import java.util.List;


/**
 * \* (ง •_•)ง,加油
 * \* User: ttm
 * \* Date: 2022/9/5
 * \* Time: 9:50
 * \* Description:
 * 1、编写程序计算n = 1、2、4、8、16、32时,以下函数的值:
 * 1、logn、n、nlogn、n2、n3、2n、n!
 * 用excel做表,保存为PDF。提交"漂亮的"表格(log以2为底)
 */
public class test1 {
    //TODO:logn2;
    public static double log2(double N) {
        return Math.log(N) / Math.log(2);  //java的Math.log的底为e
    }

    //TODO:n
    public static double n(double N) {
        return N;  //n
    }

    //TODO:n*log2
    public static double nlog(double N) {
        return N * log2(N);
    }


    //TODO:n*n
    public static double n2(double N) {
        return N * N;
    }

    //TODO:n*n*n
    public static double n3(double N) {
        return N * N * N;
    }

    //TODO:2的N次方  !!平常算法超过32有异常,这里用字符的形式进行处理
    private static String pow2(double n) {
        StringBuilder res = new StringBuilder("1");
        //StringBuilder==》一个可变的字符串类,String内容是不可变的,StringBuilder内容是可变的,所以这里用StringBuilder

        // 重复N次
        for (int i = 0; i < n; i++) {
            // 进位标志,每轮清零
            int temp = 0;
            // res中的字符,从前往后逐位*2
            for (int j = res.length() - 1; j >= 0; j--) {
                //这里进行乘法运算,需要加上进位   (<<按位左移运算符。左操作数按位左移右操作数指定的位数。)
                temp = ((res.charAt(j) - '0') << 1) + temp / 10;
                //替换此位结果
                res.setCharAt(j, (char) (temp % 10 + '0'));
            }
            //产生进位则需添加新的数字
            if (temp / 10 >= 1)
                res.insert(0, '1');
        }
        return res.toString();
    }

    //额。java好像有这个方法。。。。
    public static double Pow(double N) {
        return Math.pow(2, N);
    }

    //TODO:N!
    public static double nj(double N) {
        double n = 1;
        for (int i = 1; i <= N; i++) {
            n = n * i;
        }
        return n;
    }

    public static void main(String[] args) {
        List<Double> arrayList = new ArrayList();
        arrayList.add(1.0);
        arrayList.add(2.0);
        arrayList.add(4.0);
        arrayList.add(8.0);
        arrayList.add(16.0);
        arrayList.add(32.0);
        for (Double a : arrayList) {
            System.out.println("log2" + " " + log2(a));
            System.out.println("n" + " " + n(a));
            System.out.println("n*log2" + " " + nlog(a));
            System.out.println("n的2次方" + " " + n2(a));
            System.out.println("n的3次方" + " " + n3(a));
            System.out.println("2的n次方" + " " + pow2(a));
            System.out.println("n的阶乘" + " " + nj(a));
            System.out.println("============================");
        }
    }
}

函数

1

2

4

8

16

32

logn

0

1

2

3

4

5

n

1

2

4

8

16

32

nlogn

0

2

8

24

64

160

n2

1

4

16

64

256

1024

n3

1

8

64

512

4096

32768

2n

2

4

16

256

65536

4294967296

n!

1

2

24

40320

2.09E+13

2.63E+35

2、测试你的机器跑递归的阶乘程序,当n=?时,程序开始抛出: java.lang.StackOverflowError

注意:ppt的代码的返回值是int,能表示的数太小,要用long或double。记录下你尝试的次数。

package homework.demo01;

/**
 * \* (ง •_•)ง,加油
 * \* User: ttm
 * \* Date: 2022/9/5
 * \* Time: 11:48
 * \* Description:
 * \
 */
public class test2 {
    public static long n(long n) {
        if (n == 1) {
            return n;
        } else {
            return n = n * n(n - 1);
        }
    }

    public static void main(String[] args) {
        long i = 12080;
        System.out.println(i + "             " + n(i));
    }
}

第一次产考老师的13171,然后抛出异常,又尝试12999依旧抛出异常,通过多次折半尝试,得出在12080使会开始抛出异常。

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;

}

x的次数为:1+(1+2)+(1+2+3)+·······+(1+2+3+4+········+n-2)

 

  • 4
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值