zoukankan      html  css  js  c++  java
  • 面试题目——《CC150》数学与概率

    面试题7.2:三角形的三个顶点上各有一只蚂蚁。如果蚂蚁开始沿着三角形的边爬行,两只或三只蚂蚁撞到一起的概率有多大?假定每只蚂蚁会随机选一个方向,每个方向被选到的几率相等,而且三只蚂蚁的爬行速度相同。

    package cc150.intelligence;
    
    public class Ants {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Ants at = new Ants();
    		System.out.println(at.antsCollision(1));
    	}
    	
    	public double antsCollision(int n) {
            // write code here
    		if(n < 3 || n > 10000)
    			return 1;
    		return 1-Math.pow(0.5, (n-1));
        }
    
    }
    

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

    package cc150.intelligence;
    
    public class CrossLine {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		CrossLine cl = new CrossLine();
    		System.out.println(cl.checkCrossLine(0.48900,0.48900,0.32700,0.32700));
    	}
    	
    	public boolean checkCrossLine(double s1, double s2, double y1, double y2) {
            // write code here
    		double abs=1e-6;
    		if(Math.abs(s1-s2)<abs && Math.abs(y1-y2)>abs)	//斜率相同,截距不同
    			return false;
    		return true;
        }
    
    }
    

    面试题7.4:编写方法,实现整数的乘法、减法和除法运算。只允许使用加号。

    package cc150.intelligence;
    
    public class AddSubstitution {
    
    	public static void main(String[] args) {	
    		// TODO 自动生成的方法存根
    		
    	}
    	
    	public int calc(int a, int b, int type) {	//1是乘法,0是除法,-1是减法
            // write code here
    		if(type == 1)
    			return multiply(a,b);
    		else if(type == 0)
    			return divide(a,b);
    		else if(type == -1)
    			return minus(a,b);
    		else
    			return 0;
        }
    	
    	public int negate(int a){	//乘以-1,负号变正,正号变负
    		int neg = 0;
    		int sym = a > 0 ? -1: 1;		//判断正负
    		while(a != 0){
    			a += sym;		//a为正,sym为负;a为负,sym为正,相加直到0
    			neg += sym;
    		}
    		return neg;
    	}
    	
    	public int minus(int a,int b){	//只使用加法的减法,即连续加b次正1或者负1
    		return a + negate(b);
    	}
    	
    	public int multiply(int a,int b){	//只使用加法的乘法,b个a相加
    		if(a < b)
    			return multiply(b,a);
    		int sum = 0;
    		for(int i=Math.abs(b);i>0;i--)
    			sum += a;
    		if(b < 0)
    			sum = negate(sum);
    		return sum;
    	}
    	
    	public int divide(int a,int b) throws java.lang.ArithmeticException{	//只使用加法的除法
    		if(b == 0){
    			throw new java.lang.ArithmeticException("ERROR");
    		}
    		int absa = Math.abs(a);		//先不考虑正负的问题
    		int absb = Math.abs(b);
    		int result = 0;
    		int count = 0;
    		while(result + absb <= absa){	//循环加absb,直到超过absa,计数加了几次
    			result += absb;
    			count++;
    		}
    		if((a < 0 && b < 0) || (a > 0 && b > 0))
    			return count;
    		else
    			return negate(count);
    	}
    
    }
    

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

    package cc150.intelligence;
    
    public class Bipartition {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		Bipartition bp = new Bipartition();
    		Point[] A = {new Point(136,6278),new Point(3958,6278),new Point(3958,2456),new Point(136,2456)};
    		Point[] B = {new Point(-3898,11132),new Point(7238,11132),new Point(7238,-4),new Point(-3898,-4)};
    		
    		System.out.println(""+bp.getBipartition(A,B)[0]+","+bp.getBipartition(A,B)[1]);
    	}
    	
    	public double[] getBipartition(Point[] A, Point[] B) {
            // write code here
    		double a_x = getCenter(A)[0];	//正方形A的中点坐标
    		double a_y = getCenter(A)[1];
    		double b_x = getCenter(B)[0];	//正方形B的中点坐标
    		double b_y = getCenter(B)[1];
    		
    		double[] result = new double[2];
    		result[0] = (a_y-b_y)/(a_x-b_x);
    		double min = 10e-6;
    		if(Math.abs(result[0]) < min)
    			result[0] = 0;
    		result[1] =a_y - result[0] * a_x;
    		return result;
        }
    	
    	public double[] getCenter(Point[] p){	//返回一个正方形的中点坐标
    		double[] re= {(p[2].x+p[3].x)/2.0,(p[0].y+p[3].y)/2.0};		//注意是double类型的,要除以2.0
    		return re;
    	}
    	
    	
    	
    	public static 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;
    	    }
    	}
    
    }
    

    面试题7.6:在二维平面上,有一些点,请找出经过点数最多的那条线。

    面试题7.7:有些数的素因子只有3、5、7,请设计一个算法,找出其中第k个数。

    package cc150.intelligence;
    
    import java.util.LinkedList;
    import java.util.Queue;
    
    public class KthNumber {
    
    	public static void main(String[] args) {
    		// TODO 自动生成的方法存根
    		KthNumber kn = new KthNumber();
    		System.out.println(kn.findKth(0));
    	}
    	
    	public int findKth(int k) {
            // write code here
    		if(k < 0)
    			return 0;
    		int val = 0;		//存放3,5,7的倍数中的最小值
    		Queue<Integer> queue3 = new LinkedList<Integer>();	//存放3的倍数的队列
    		Queue<Integer> queue5 = new LinkedList<Integer>();
    		Queue<Integer> queue7 = new LinkedList<Integer>();
    		queue3.add(1);			//一定要先放进一个1,否则v3为Integer.MAX_VALUE
    		
    		for(int i=0;i<=k;i++){
    			int v3 = queue3.size() > 0 ? queue3.peek():Integer.MAX_VALUE;	//如果队列不为空的话,取得队列的头,即最小值
    			int v5 = queue5.size() > 0 ? queue5.peek():Integer.MAX_VALUE;
    			int v7 = queue7.size() > 0 ? queue7.peek():Integer.MAX_VALUE;
    			val = Math.min(v3, Math.min(v5, v7));
    			
    			if(val == v3){	//求出了最小值,原本队列的队首要移除
    				queue3.remove();
    				queue3.add(val * 3);
    				queue5.add(val * 5);
    			}else if(val == v5){
    				queue5.remove();
    				queue5.add(val * 5);
    			}else if(val == v7){
    				queue7.remove();
    			}
    			queue7.add(val * 7);
    		}
    		return val;
        }
    
    }
    
  • 相关阅读:
    高性能可扩展mysql 笔记(一)数据库表、索引、SQL语句设计规范
    Mybatis 的动态SQL,批量增删查改
    数据库之 MySQL --- 视图的原理解析与创建(八)
    8.0 以上版本 mySQL数据库导致的命令行可连接,NaviCat不可连接的问题
    数据库之 MySQL --- 数据处理 之 表的约束与分页(七)
    数据库之 MySQL --- 数据处理 之 表操作、CRUD(六)
    数据库之 MySQL --- 数据处理 之 多行子查询(五)
    数据库之 MySQL --- 数据处理 之 单行函数、组函数 (四)
    数据库之 MySQL --- 数据处理 之多表查询 (三)
    数据库之 MySQL --- 数据处理 之 子查询 (二)
  • 原文地址:https://www.cnblogs.com/tonglin0325/p/5904326.html
Copyright © 2011-2022 走看看