1. 程式人生 > >機器學習面試程式設計題彙總

機器學習面試程式設計題彙總

阿里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;
    }
}