机器学习面试编程题汇总

阿里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;
    }
}
Programming Exercise 1: Linear Regression Machine Learning Introduction In this exercise, you will implement linear regression and get to see it work on data. Before starting on this programming exercise, we strongly recom- mend watching the video lectures and completing the review questions for the associated topics. To get started with the exercise, you will need to download the starter code and unzip its contents to the directory where you wish to complete the exercise. If needed, use the cd command in Octave/MATLAB to change to this directory before starting this exercise. You can also find instructions for installing Octave/MATLAB in the “En- vironment Setup Instructions” of the course website. Files included in this exercise ex1.m - Octave/MATLAB script that steps you through the exercise ex1 multi.m - Octave/MATLAB script for the later parts of the exercise ex1data1.txt - Dataset for linear regression with one variable ex1data2.txt - Dataset for linear regression with multiple variables submit.m - Submission script that sends your solutions to our servers [?] warmUpExercise.m - Simple example function in Octave/MATLAB [?] plotData.m - Function to display the dataset [?] computeCost.m - Function to compute the cost of linear regression [?] gradientDescent.m - Function to run gradient descent [†] computeCostMulti.m - Cost function for multiple variables [†] gradientDescentMulti.m - Gradient descent for multiple variables [†] featureNormalize.m - Function to normalize features [†] normalEqn.m - Function to compute the normal equations ? indicates files you will need to complete † indicates optional exercises
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值