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

  1. package cc150.intelligence;
  2.  
  3. public class Ants {
  4.  
  5. public static void main(String[] args) {
  6. // TODO 自动生成的方法存根
  7. Ants at = new Ants();
  8. System.out.println(at.antsCollision(1));
  9. }
  10.  
  11. public double antsCollision(int n) {
  12. // write code here
  13. if(n < 3 || n > 10000)
  14. return 1;
  15. return 1-Math.pow(0.5, (n-1));
  16. }
  17.  
  18. }

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

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

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

  1. package cc150.intelligence;
  2.  
  3. public class AddSubstitution {
  4.  
  5. public static void main(String[] args) {
  6. // TODO 自动生成的方法存根
  7.  
  8. }
  9.  
  10. public int calc(int a, int b, int type) { //1是乘法,0是除法,-1是减法
  11. // write code here
  12. if(type == 1)
  13. return multiply(a,b);
  14. else if(type == 0)
  15. return divide(a,b);
  16. else if(type == -1)
  17. return minus(a,b);
  18. else
  19. return 0;
  20. }
  21.  
  22. public int negate(int a){ //乘以-1,负号变正,正号变负
  23. int neg = 0;
  24. int sym = a > 0 ? -1: 1; //判断正负
  25. while(a != 0){
  26. a += sym; //a为正,sym为负;a为负,sym为正,相加直到0
  27. neg += sym;
  28. }
  29. return neg;
  30. }
  31.  
  32. public int minus(int a,int b){ //只使用加法的减法,即连续加b次正1或者负1
  33. return a + negate(b);
  34. }
  35.  
  36. public int multiply(int a,int b){ //只使用加法的乘法,b个a相加
  37. if(a < b)
  38. return multiply(b,a);
  39. int sum = 0;
  40. for(int i=Math.abs(b);i>0;i--)
  41. sum += a;
  42. if(b < 0)
  43. sum = negate(sum);
  44. return sum;
  45. }
  46.  
  47. public int divide(int a,int b) throws java.lang.ArithmeticException{ //只使用加法的除法
  48. if(b == 0){
  49. throw new java.lang.ArithmeticException("ERROR");
  50. }
  51. int absa = Math.abs(a); //先不考虑正负的问题
  52. int absb = Math.abs(b);
  53. int result = 0;
  54. int count = 0;
  55. while(result + absb <= absa){ //循环加absb,直到超过absa,计数加了几次
  56. result += absb;
  57. count++;
  58. }
  59. if((a < 0 && b < 0) || (a > 0 && b > 0))
  60. return count;
  61. else
  62. return negate(count);
  63. }
  64.  
  65. }

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

  1. package cc150.intelligence;
  2.  
  3. public class Bipartition {
  4.  
  5. public static void main(String[] args) {
  6. // TODO 自动生成的方法存根
  7. Bipartition bp = new Bipartition();
  8. Point[] A = {new Point(136,6278),new Point(3958,6278),new Point(3958,2456),new Point(136,2456)};
  9. Point[] B = {new Point(-3898,11132),new Point(7238,11132),new Point(7238,-4),new Point(-3898,-4)};
  10.  
  11. System.out.println(""+bp.getBipartition(A,B)[0]+","+bp.getBipartition(A,B)[1]);
  12. }
  13.  
  14. public double[] getBipartition(Point[] A, Point[] B) {
  15. // write code here
  16. double a_x = getCenter(A)[0]; //正方形A的中点坐标
  17. double a_y = getCenter(A)[1];
  18. double b_x = getCenter(B)[0]; //正方形B的中点坐标
  19. double b_y = getCenter(B)[1];
  20.  
  21. double[] result = new double[2];
  22. result[0] = (a_y-b_y)/(a_x-b_x);
  23. double min = 10e-6;
  24. if(Math.abs(result[0]) < min)
  25. result[0] = 0;
  26. result[1] =a_y - result[0] * a_x;
  27. return result;
  28. }
  29.  
  30. public double[] getCenter(Point[] p){ //返回一个正方形的中点坐标
  31. double[] re= {(p[2].x+p[3].x)/2.0,(p[0].y+p[3].y)/2.0}; //注意是double类型的,要除以2.0
  32. return re;
  33. }
  34.  
  35. public static class Point {
  36. int x;
  37. int y;
  38. public Point(int x, int y) {
  39. this.x = x;
  40. this.y = y;
  41. }
  42. public Point() {
  43. this.x = 0;
  44. this.y = 0;
  45. }
  46. }
  47.  
  48. }

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

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

  1. package cc150.intelligence;
  2.  
  3. import java.util.LinkedList;
  4. import java.util.Queue;
  5.  
  6. public class KthNumber {
  7.  
  8. public static void main(String[] args) {
  9. // TODO 自动生成的方法存根
  10. KthNumber kn = new KthNumber();
  11. System.out.println(kn.findKth(0));
  12. }
  13.  
  14. public int findKth(int k) {
  15. // write code here
  16. if(k < 0)
  17. return 0;
  18. int val = 0; //存放3,5,7的倍数中的最小值
  19. Queue<Integer> queue3 = new LinkedList<Integer>(); //存放3的倍数的队列
  20. Queue<Integer> queue5 = new LinkedList<Integer>();
  21. Queue<Integer> queue7 = new LinkedList<Integer>();
  22. queue3.add(1); //一定要先放进一个1,否则v3为Integer.MAX_VALUE
  23.  
  24. for(int i=0;i<=k;i++){
  25. int v3 = queue3.size() > 0 ? queue3.peek():Integer.MAX_VALUE; //如果队列不为空的话,取得队列的头,即最小值
  26. int v5 = queue5.size() > 0 ? queue5.peek():Integer.MAX_VALUE;
  27. int v7 = queue7.size() > 0 ? queue7.peek():Integer.MAX_VALUE;
  28. val = Math.min(v3, Math.min(v5, v7));
  29.  
  30. if(val == v3){ //求出了最小值,原本队列的队首要移除
  31. queue3.remove();
  32. queue3.add(val * 3);
  33. queue5.add(val * 5);
  34. }else if(val == v5){
  35. queue5.remove();
  36. queue5.add(val * 5);
  37. }else if(val == v7){
  38. queue7.remove();
  39. }
  40. queue7.add(val * 7);
  41. }
  42. return val;
  43. }
  44.  
  45. }

面试题目——《CC150》数学与概率的更多相关文章

  1. Java高概率面试题目—finally

    在Java面试中关于finally的面试题目出现的概率非常高,而且一旦面试官问起绝不会是蜻蜓点水,而是会向你发起层层递进地“连环问”,并且回答这系列问题常常需要代码的辅助,可谓考验基础的面试利题.究竟 ...

  2. C语言经典面试题目(转的,不过写的的确好!)

    第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...

  3. 【转】React 常用面试题目与分析

    作者:王下邀月熊链接:https://zhuanlan.zhihu.com/p/24856035来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文有一定概率为水文,怕 ...

  4. 全网最全C#实习面试题目

    整个内容是我在春招面试时候整理的一些题目,里面涵盖有网上搬运的(由于当时没有记录来源,如果有转载没标注来源,请与我联系),还有我面试到的.整个排版很乱,后期我会一步一步整理.整个内容大概快有两万字.整 ...

  5. HTML/CS3相关面试题目

    一.HTML/CS3基本面试题目. 1. 常用那几种浏览器测试? 1.1浏览器:IE,Chrome(谷歌),FireFox(火狐),Safari(苹果计算机的最新操作系统Mac OS X中的浏览器,使 ...

  6. PHP面试题目搜集

    搜集这些题目是想在学习PHP方面知识有更感性的认识,单纯看书的话会很容易看后就忘记. 曾经看过数据结构.设计模式.HTTP等方面的书籍,但是基本看完后就是看完了,没有然后了,随着时间的推移,也就渐渐忘 ...

  7. 总结CSS面试题目的考察点及常见布局问题整理

    整理网上流传的若干份面试题目,突发奇想,总结关于CSS面试题目的考察点,发现问题大多围绕几个属性和几种题目,水平有限,仅供参考. 写这个博文内心有种莫名奇妙的自我谴责感,实在不应该把面试层叠样式“应试 ...

  8. 【转】asp.net c# 网上搜集面试题目大全(附答案)

    asp.net c# 网上搜集面试题目大全(附答案) http://www.cnblogs.com/hndy/articles/2234188.html

  9. 2016年Web前端面试题目汇总

    转载: 2016年Web前端面试题目汇总 以下是收集一些面试中经常会遇到的经典面试题以及自己面试过程中未解决的问题,通过对知识的整理以及经验的总结,重新巩固自身的前端基础知识,如有错误或更好的答案,欢 ...

  10. 33条C#、.Net经典面试题目及答案

    33条C#..Net经典面试题目及答案[zt] 本文集中了多条常见的C#..Net经典面试题目例如".NET中类和结构的区别"."ASP.NET页面之间传递值的几种方式? ...

随机推荐

  1. HDFS Federation (读书笔记)

    HDFS Federation (读书笔记) HDFS的架构 HDFS包含两个层次:命名空间管理(Namespace) 和 块/存储管理(Block Storage). 命名空间管理(Namespac ...

  2. MyEclipse中没有自动提示如何设置

    步骤: window->Preferences->Java->Editor->Content Assist->Advanced 将该页面上的所有的未打上勾的选项都勾上就可 ...

  3. java实现文件变化监控

    一. spring配置文件:application.xml <?xml version="1.0" encoding="UTF-8"?> <b ...

  4. 算法: 斐波那契数列C/C++实现

    斐波那契数列: 1,1,2,3,5,8,13,21,34,....     //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...

  5. Ubuntu下Apache+SVN+submin实现WEB管理SVN

    为什么需要submin管理SVN? 原来在Ubuntu下,都是直接通过命令行创建SVN仓库并分配权限,但是这有一些问题: 每创建一个SVN仓库,都需要修改httpd.conf 每创建一个帐户,都需要手 ...

  6. WPF 自定义ListBox

     如题,要实现一个如下的列表,该如何实现? 在设计过程中,会遇到如下问题: 1.ListBox中ListBoxItem的模板设计 2.ListBox中ListBoxItem的模板容器设计 3.List ...

  7. STM32 KEIL不能输入仿真引脚端口error 65: access violation at 0x40021000 : no 'read' permission

    使用MDK自己创建一个STM32F103ZE核的项目 加入源码后编译,正常,在线仿真单步执行出现如下问题 error 65: access violation at 0x40021000 : no ' ...

  8. android nagative drawer图标跟标题适配

    <?xml version="1.0" encoding="utf-8"?> <resources> <string name=& ...

  9. [转]17个新手常见Python运行时错误

    原址:http://www.oschina.net/question/89964_62779?p=1 当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的 ...

  10. winform/窗体鼠标事件编程中的几个问题

    1.进行.net窗体的开发,经常用到鼠标事件,如MouseDown/MouseUp/MouseMove/MouseClick等.可是有时候给控件添加鼠标事件,就是不响应,怎么办呢! 答案:1.控件是否 ...