算法分析作业;漂亮的表格&&++x

1、编写程序计算n = 1、2、4、8、16、32时,以下函数的值:
1、logn、n、nlogn、n2、n3、2n、n!
用excel做表,保存为PDF。提交"漂亮的"表格(log以2为底)

2、请给出++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;
}

public class Text_02 {
    private final double[] n={1,2,4,8,16,32};
    public double[] logn(){
        double []newn = new double[n.length];
        for (int i = 0; i < n.length; i++)
            newn[i] =Math.log(n[i])/Math.log(2);
        return newn;
    }
    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;
    }
    public double[] nlogn(){
        double [] newn = logn();
        for (int i = 0; i < n.length; i++)
            newn[i] *= n[i];
        return newn;
    }
    public double[] c(){return getDoubles(0);}
    public double[] dn(){return getDoubles(2);}
    public double[] tn(){return getDoubles(3);}
    public double[] index2(){
        double[] newn  = new double[n.length];
        for (int i = 0; i <n.length;i++)
            newn[i] =Math.pow(2,n[i]);
        return newn;
    }
    public double[] factorial(){
        double[] newn  = new double[n.length];
        for (int i = 0; i < n.length; i++)
            newn[i] = recursion(n[i]);
        return newn;
    }
    public double recursion(double num){
        if (num == 1)
            return 1;
        else
            return num*recursion(num-1);
    }
    private double[] getDoubles(int x) {
        double[] newn = new double[n.length];
        for (int i = 0; i < n.length; i++)
            newn[i] = Math.pow(n[i], x);
        return newn;
    }
    private static void print(double[] doubles) {
        for (double x : doubles)
            System.out.print(x + " ");
        System.out.println("\n");
    }
}

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

  • 4
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Xlorb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值