zoukankan      html  css  js  c++  java
  • 程序员面试——数学和概率

      第七章

    7.1 投篮概率

    写数学式子就好,基本概念。

    7.2 碰撞的蚂蚁

    在n个顶点的多边形上有n只蚂蚁,这些蚂蚁同时开始沿着多边形的边爬行,请求出这些蚂蚁相撞的概率。(这里的相撞是指存在任意两只蚂蚁会相撞)

    给定一个int n(3<=n<=10000),代表n边形和n只蚂蚁,请返回一个double,为相撞的概率。

    测试样例:
    3
    返回:0.75

    先算出数学表达是什么,然后实现;

    注意:JAVA的平方:Math.pow(x,n)

    import java.util.*;
    
    public class Ants {
        public double antsCollision(int n) {
            double sq=Math.pow(0.5,(n-1));
            return (1-sq);
        }
    }

    7.3 判断直线相交

    给定直角坐标系上的两条直线,确定这两条直线会不会相交。

    线段以斜率和截距的形式给出,即double s1,double s2,doubley1,double y2,分别代表直线1和2的斜率(即s1,s2)和截距(即y1,y2),请返回一个bool,代表给定的两条直线是否相交。这里两直线重合也认为相交。

    测试样例:
    3.14,1,3.14,2
    返回:false

    关键是重合要考虑到;

    import java.util.*;
    
    public class CrossLine {
        public boolean checkCrossLine(double s1, double s2, double y1, double y2) {
            if (s1==s2) {
                if (y1==y2) {
                    return true;
                }
                return false;
            }
            return true;
        }
    }

    7.4 加号实现运算

    这是别个的。

    import java.util.*;
     
    public class AddSubstitution {
        public int calc(int a, int b, int type) {
            int sum=0;//这里统一一个变量就可以了,不用定义好几个变量
            switch(type){
            case 1:
                while(b>0){//Java中不允许用while(b--)c++里面认为b>0为true,b==0为false,Java里面while只能放布尔类型表达式
                    sum+=a;
                    b--;
                }
                return sum;//也可以每个case用break;最后统一 return sum;      
            case 0:
                while(a>b){
                    a+=(-b);
                    sum++;
                }
                return sum;
            case -1:
                sum=a+(-b);
                return sum;
            default://如果type不是1、-1、0;则返回-1表示输入有错
                return -1;
            }
        }
    }
    import java.util.*;
    
    public class AddSubstitution {
        public int calc(int a, int b, int type) {
            int res=0;
            switch(type){
                case 1: res=multi(a,b);
                case 0: res=dev(a,b);
                default:res=minus(a,b);
            }
            return res;
        }
        
        //需要考虑正负号问题
        int multi(int a,int b){
            if(a==0 || b==0) return 0;
            int sign=0;
            if(a<0 || b<0){
                if (a<0 && b<0) {
                    sign=1;
                }
                sign=0;
            }
            a=Math.abs(a);
            b=Math.abs(b);
            int sum=0;
            for(int i=0;i<b;i++) {
                sum+=a;
            }
            b=sum;
            int neg=0;
            if(sign==0){
                for(int i=0;i<b;i++){
                    sum--;
                    neg--;
                }
                return neg;
            }
            return sum;
        }
        
        int dev(int a,int b){
            int sign=0;
            if(a<0 || b<0){
                if (a<0 && b<0) {
                    sign=1;
                }
                sign=0;
            }
            int absa=Math.abs(a);
            int absb=Math.abs(b);
            int neg=0;
            int count=0;
            //int bei=absb;
            
            if(absa<absb) return 0;
            while(absa>0){
                absa=absa-absb;
                count++;
            }
            
            int index=count;
            if(sign==0){
                for(int i=0;i<index;i++){
                    count--;
                    neg--;
                }
                return neg;
            }
            return count;
            
        }
        
        int minus(int a,int b){
            int negb=0;
            int index=b;
            int d=(b>0)?(-1):1;
            for(int i=0;i<index;i++) {
                negb+=d;
                b+=d;
            }
            return a+negb;
        }
        
    }

    我的,。。。。。不知道哪里有错误。。。。

    7.5 平分的直线

    在二维平面上,有两个正方形,请找出一条直线,能够将这两个正方形对半分。假定正方形的上下两条边与x轴平行。

    给定两个vecotrAB,分别为两个正方形的四个顶点。请返回一个vector,代表所求的平分直线的斜率和截距,保证斜率存在。

    测试样例:
    [(0,0),(0,1),(1,1),(1,0)],[(1,0),(1,1),(2,0),(2,1)]
    返回:[0.0,0.5]

    java的class运用啊,A[0].x

    import java.util.*;
     
    /*
    public class Point {
        int x;
        int y;
        public Point(int x, int y) {
            this.x = x;
            this.y = y;
        }
        public Point() {
            this.x = 0;
            this.y = 0;
        }
    }*/
    public class Bipartition {
        public double[] getBipartition(Point[] A, Point[] B) {
            double[] result=new double[2];
            result[0]=(center(A)[1]-center(B)[1])/(center(A)[0]-center(B)[0]);//k=(y1-y2)/(x1-x2)
            if(Math.abs(result[0])==0)
                result[0]=0.0;
            result[1]=center(A)[1]-result[0]*center(A)[0];//b=y1-k*x1
            return result;
        }
        public double[] center(Point[] a){
            double[] result={0,0};
            for(int i=0;i<a.length;i++){
                result[0]+=a[i].x;
                result[1]+=a[i].y;        
            }
            result[0]/=4;
            result[1]/=4;
            return result;
        }
    }

    7.6 7.7 不懂,之后再做

    --------------拓展--------------------

    17.11 rand(5)——》rand7()

    等价对换就好,但实现的话,又有很多细节需要注意!

    18.2 完美洗牌

    完全没什么思路啊。。。。。。。。。。。

    书上是完全打乱n-1的,然后最后一张与前面随机一张交换顺序;

    按照这个思路就是直接的递归。。

  • 相关阅读:
    dataframe字段过长被截断
    sublime text 3安装Anaconda插件之后写python出现白框
    在tkinter中使用matplotlib
    RemoteDisconnected: Remote end closed connection without response
    object of type 'Response' has no len()
    matploylib之热力图
    pycharm格式化python代码快捷键Ctrl+Alt+L失效
    Windows下Redis集群配置
    七牛云--对象存储
    Spring发送邮件
  • 原文地址:https://www.cnblogs.com/andy1202go/p/5765415.html
Copyright © 2011-2022 走看看