面试题目——《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;
- }
- }
面试题目——《CC150》数学与概率的更多相关文章
- Java高概率面试题目—finally
在Java面试中关于finally的面试题目出现的概率非常高,而且一旦面试官问起绝不会是蜻蜓点水,而是会向你发起层层递进地“连环问”,并且回答这系列问题常常需要代码的辅助,可谓考验基础的面试利题.究竟 ...
- C语言经典面试题目(转的,不过写的的确好!)
第一部分:基本概念及其它问答题 1.关键字static的作用是什么? 这个简单的问题很少有人能回答完全.在C语言中,关键字static有三个明显的作用: 1). 在函数体,一个被声明为静态的变量在这一 ...
- 【转】React 常用面试题目与分析
作者:王下邀月熊链接:https://zhuanlan.zhihu.com/p/24856035来源:知乎著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处. 本文有一定概率为水文,怕 ...
- 全网最全C#实习面试题目
整个内容是我在春招面试时候整理的一些题目,里面涵盖有网上搬运的(由于当时没有记录来源,如果有转载没标注来源,请与我联系),还有我面试到的.整个排版很乱,后期我会一步一步整理.整个内容大概快有两万字.整 ...
- HTML/CS3相关面试题目
一.HTML/CS3基本面试题目. 1. 常用那几种浏览器测试? 1.1浏览器:IE,Chrome(谷歌),FireFox(火狐),Safari(苹果计算机的最新操作系统Mac OS X中的浏览器,使 ...
- PHP面试题目搜集
搜集这些题目是想在学习PHP方面知识有更感性的认识,单纯看书的话会很容易看后就忘记. 曾经看过数据结构.设计模式.HTTP等方面的书籍,但是基本看完后就是看完了,没有然后了,随着时间的推移,也就渐渐忘 ...
- 总结CSS面试题目的考察点及常见布局问题整理
整理网上流传的若干份面试题目,突发奇想,总结关于CSS面试题目的考察点,发现问题大多围绕几个属性和几种题目,水平有限,仅供参考. 写这个博文内心有种莫名奇妙的自我谴责感,实在不应该把面试层叠样式“应试 ...
- 【转】asp.net c# 网上搜集面试题目大全(附答案)
asp.net c# 网上搜集面试题目大全(附答案) http://www.cnblogs.com/hndy/articles/2234188.html
- 2016年Web前端面试题目汇总
转载: 2016年Web前端面试题目汇总 以下是收集一些面试中经常会遇到的经典面试题以及自己面试过程中未解决的问题,通过对知识的整理以及经验的总结,重新巩固自身的前端基础知识,如有错误或更好的答案,欢 ...
- 33条C#、.Net经典面试题目及答案
33条C#..Net经典面试题目及答案[zt] 本文集中了多条常见的C#..Net经典面试题目例如".NET中类和结构的区别"."ASP.NET页面之间传递值的几种方式? ...
随机推荐
- HDFS Federation (读书笔记)
HDFS Federation (读书笔记) HDFS的架构 HDFS包含两个层次:命名空间管理(Namespace) 和 块/存储管理(Block Storage). 命名空间管理(Namespac ...
- MyEclipse中没有自动提示如何设置
步骤: window->Preferences->Java->Editor->Content Assist->Advanced 将该页面上的所有的未打上勾的选项都勾上就可 ...
- java实现文件变化监控
一. spring配置文件:application.xml <?xml version="1.0" encoding="UTF-8"?> <b ...
- 算法: 斐波那契数列C/C++实现
斐波那契数列: 1,1,2,3,5,8,13,21,34,.... //求斐波那契数列第n项的值 //1,1,2,3,5,8,13,21,34... //1.递归: //缺点:当n过大时,递归 ...
- Ubuntu下Apache+SVN+submin实现WEB管理SVN
为什么需要submin管理SVN? 原来在Ubuntu下,都是直接通过命令行创建SVN仓库并分配权限,但是这有一些问题: 每创建一个SVN仓库,都需要修改httpd.conf 每创建一个帐户,都需要手 ...
- WPF 自定义ListBox
如题,要实现一个如下的列表,该如何实现? 在设计过程中,会遇到如下问题: 1.ListBox中ListBoxItem的模板设计 2.ListBox中ListBoxItem的模板容器设计 3.List ...
- STM32 KEIL不能输入仿真引脚端口error 65: access violation at 0x40021000 : no 'read' permission
使用MDK自己创建一个STM32F103ZE核的项目 加入源码后编译,正常,在线仿真单步执行出现如下问题 error 65: access violation at 0x40021000 : no ' ...
- android nagative drawer图标跟标题适配
<?xml version="1.0" encoding="utf-8"?> <resources> <string name=& ...
- [转]17个新手常见Python运行时错误
原址:http://www.oschina.net/question/89964_62779?p=1 当初学 Python 时,想要弄懂 Python 的错误信息的含义可能有点复杂.这里列出了常见的的 ...
- winform/窗体鼠标事件编程中的几个问题
1.进行.net窗体的开发,经常用到鼠标事件,如MouseDown/MouseUp/MouseMove/MouseClick等.可是有时候给控件添加鼠标事件,就是不响应,怎么办呢! 答案:1.控件是否 ...