机器学习面试编程题汇总

来源:互联网 发布:淘宝在哪里搜店铺? 编辑:程序博客网 时间:2024/04/30 04:16

阿里2017年3月在线编程题
这里写图片描述

package yuyin.chuli;import java.math.BigDecimal;import java.util.Scanner;public class Main {    /** 请完成下面这个函数,实现题目要求的功能 **/    /** 当然,你也可以不按照这个模板来作答,完全按照自己的想法来 ^-^ **/    static double leartCurve(double mu1, double sigma1, double mu2,            double sigma2) {        double x;        double y;        int n = 0;        double re = 0.0;        double tmp;        for (int i = 0; i <= 10000; i++) {            x = normalRandom(mu1, sigma1);            System.out.println(x);            y = normalRandom(mu2, sigma2);            tmp = Math.pow(Math.pow(x, 2) + Math.pow(y, 2) - 1, 2)                    - Math.pow(x, 2) * Math.pow(y, 2);            if (tmp > 0) {                n++;            }        }        re = n / 10000.0;        BigDecimal bd = new BigDecimal(re);        bd = bd.setScale(1, BigDecimal.ROUND_HALF_UP);        return Double.parseDouble(bd.toString());    }    public static double normalRandom(double a, double b) {        double f = 0;        double c0 = 2.515517, c1 = 0.802853, c2 = 0.010328;        double d1 = 1.432788, d2 = 0.189269, d3 = 0.001308;        double w;        double r = Math.random();        if (r <= 0.5)            w = r;        else            w = 1 - r;        if ((r - 0.5) > 0)            f = 1;        else if ((r - 0.5) < 0)            f = -1;        double y = Math.sqrt((-2) * Math.log(w));        double x = f                * (y - (c0 + c1 * y + c2 * y * y)                        / (1 + d1 * y + d2 * y * y + d3 * y * y * y));        double z = a + x * Math.sqrt(b);        return (z);    }    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        double res;        double _mu1;        _mu1 = Double.parseDouble(in.nextLine().trim());        double _sigma1;        _sigma1 = Double.parseDouble(in.nextLine().trim());        double _mu2;        _mu2 = Double.parseDouble(in.nextLine().trim());        double _sigma2;        _sigma2 = Double.parseDouble(in.nextLine().trim());        res = leartCurve(_mu1, _sigma1, _mu2, _sigma2);        System.out.println(String.valueOf(res));    }}

编程题
这里写图片描述

package yuyin.test;import java.util.HashMap;import java.util.Random;import java.util.Scanner;//n个相亲名单,每次随机一个,已经约过的丢失,继续随机约,求平均多少次才能面完所有姑娘public class M01 {    public static void main(String[] args) {        Scanner in = new Scanner(System.in);        //总次数        double all_result=0;        //每次的次数        int result=0;        //每次相亲多少个        int n= Integer.parseInt(in.nextLine().trim());        for (int i = 0; i < 10000; i++) {            result=One(n);            all_result+=result;        }        //平均次数        double avg=all_result/10000 ;        System.out.println(avg);    }    private static int One(int n) {        if (n==1) {            return 1;        }        //初始n个姑娘        //已经约会过的女孩        Random rand = new Random();        int old = rand.nextInt(n);         //开始约会  //未约会列表        HashMap<Integer, Integer> m=new HashMap<Integer, Integer>();        for (int i = old+1; i < n; i++) {            m.put(i, 1);        }        int count=0;        while (m.size()>0) {            int start = rand.nextInt(n);             if (start>old) {                m.remove(start);            }            count+=1;        }//      System.out.println(count);        return count;    }}
2 0
原创粉丝点击