算法与数据结构新手班第2节-创新互联

学会制作对数器 题目一

假设有一个数组arr,用户总是频繁的查询arr中某一段的累加和

10年积累的成都网站建设、网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先做网站设计后付款的网站建设流程,更有龙凤免费网站建设让你可以放心的选择与我们合作。

你如何组织数据,能让这种查询变得便利和快捷?

public class Main {
    public static void main(String[] args) {
        //假设有一个数组arr,用户总是频繁的查询arr中某一段的累加和
        //你如何组织数据,能让这种查询变得便利和快捷?
        int[] arr = {4,5,6,8,90};
        //前缀数字,访问一次计算一次
        System.out.println(arrSum(arr,2,4));
        //提前将全部可能创建好,访问时无需计算
        System.out.println(arrSUM(arr,2,4));
    }
    public static long arrSum(int[] arr,int begin,int end){
        if(begin>end)
            throw new RuntimeException("您的始末数字异常!");
        int sum = 0;
        int[] a =new int[arr.length];
        for(int i = 0;iend)
            throw new RuntimeException("您的始末数字异常!");
        int[][] a = new int[arr.length][arr.length];
        for(int j = 0;j=j)
                a[j][i]=a[j][i-1]+a[i][i];
            }
        }
        return a[begin][end];
    }

}
题目二

如果利用Math.random()函数,

把得到[0,x)范围上的数的概率从x调整成x^2

public class Main {

    static long count = 0;

    public static void main(String[] args) {
        for (long i = 0; i< 10000000; i++) {
            f(0.9);
        }
        System.out.println((double) count / 10000000);
    }

    public static void f(double x) {
        if (x >Math.max(Math.random(), Math.random()))
            count++;
    }
}
题目三

从1~5随机到1~7随机

从a~b随机到c~d随机

01不等概率随机到01等概率随机

public class Main {
    public static void main(String[] args) {
        int count = 0;
        for (int i = 0; i< 1000000; i++) {
            if (f5() == 0)
                count++;
        }
        System.out.println((double) count / 1000000);

    }

    public static int f1() {
        //1~5随机
        return (int) (Math.random() * 5) + 1;
    }

    public static int f2_1() {
        //0/1等概率发生器
        int a = 0;
        do {
            a = f1();
        } while (a == 3);
        return a< 3 ? 0 : 1;
    }

    public static int f2() {
        //1~7随机
        int a = 0;
        do {
            a = (f2_1()<< 2) + (f2_1()<< 1) + f2_1();
        } while (a == 7);
        return a + 1;
    }

    public static int f3() {
        //4~87随机
        int a = 0;
        do {
            a = (f2_1()<< 6) + (f2_1()<< 5) + (f2_1()<< 4) + (f2_1()<< 3) + (f2_1()<< 2) + (f2_1()<< 1) + f2_1();
        } while (a >87 || a< 4);
        return a;
    }

    public static int f4() {
        //0/1不等概率发生器
        if (f1() >2)
            return 0;
        else
            return 1;
    }

    public static int f5() {
        //利用0/1不等概率发生器生成0/1随机生成器,进而可以设计其他生成器
        int a = 0;
        int b = 0;
        do {
            a = f4();
            b = f4();
        } while ((a == 1 && b == 1) || (a == 0 && b == 0));
        if (a == 1 && b == 0)
            return 0;
        else
            return 1;
    }
}

你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧


分享名称:算法与数据结构新手班第2节-创新互联
标题链接:http://bzwzjz.com/article/ecjeh.html

其他资讯

Copyright © 2007-2020 广东宝晨空调科技有限公司 All Rights Reserved 粤ICP备2022107769号
友情链接: 重庆网站建设 成都网站建设推广 企业网站建设 成都网站建设 网站建设公司 成都网站建设 网站建设改版 网站建设 成都商城网站建设 H5网站制作 网站建设方案 成都网站建设公司 成都营销网站建设 手机网站建设 网站建设推广 网站设计公司 四川成都网站制作 成都网站建设公司 高端品牌网站建设 专业网站设计 成都营销网站制作 成都网站建设公司