Amazon2014在线笔试 第三题
问题描述:

算法分析:
s1:层数对齐:分别求两个数所在的层(l1,l2),把层数大的(假设l2>l1)先往上找父节点,从而对齐到l1层;
s2:两个数同时往上找, 直到找到公共的父节点(一定能找到,最坏情况下是0), 算法结束。
因此算法的核心就变成了如何找父节点, 求父节点的分析:
求父节点的时候,总是发现没有规律可循, 最重要的原因就是树中节点序号的增加是一个s型的路线,那么我们是利用这种特殊性
(s型),还是去除这种特殊性;我暂时没有发现如何利用这种特殊性, 所以我就先把树中节点的序号按从左到右的顺序递增,发现这样求
父节点的规律就很明显了,所以就采用了"去除这种特殊性"的方法。那么如何去除:每行中间位置上的节点的序号是有规律的,所以通过中
间数,和求得的父节点序号(不是真实的父节点序号, 如:17/3 == 5, 但实际上17的父节点是11,不是5),得到真实的父节点序号。
用一个例子来说明, 比如求17的父节点序号:
先直接求父节点序号(假的):17/3==5,求父节点所在层(2层)中间节点的序号为8, 所以真实的父节点序号是:8*2-5=11]
//File: Solution.java
//Author: lxw
//Time: 2014-10-09
//Usage: Get the common ancestor.
//Method: Align the layers of the 2 numbers. Decrease both of the layers to find the common ancestor.
//NOTE that each time the layer is decreased, we need to convert the number. import java.util.*;
public class Solution{
//I really do not think this function uses a good altorithm.
private static int getLayer(int num){
int res = 1;
int last = 0;
int next = 0;
if(num == 0){
return 0;
}
last = (int)((Math.pow(3, res) - 3.0) / 2.0);
while(true){
next = (int)((Math.pow(3, res+1) - 3.0) / 2.0);
if(num > last && num <= next){
return res;
}
++res;
last = next;
}
} private static int getMid(int layer){
if(layer > 0){
int sum = 0;
for(int i = 1; i < layer; ++i){
sum += Math.pow(3, i);
}
sum += (int)((Math.pow(3, layer) + 1.0) / 2.0);
return sum;
}
else{
return 0;
}
} //num2Lay >= num1Lay
private static int getCommon(int num2Lay, int num1Lay, int num2, int num1){
//层数对齐
while(num1Lay != num2Lay){
if(num2 % 3 == 0){
--num2;
}
num2 /= 3;
--num2Lay;
//Each time the layer is decreased, we need to convert the number
num2 = 2 * getMid(num2Lay) - num2;
} //一起往上找父节点
if(num2 == num1){
return num1; //final answer
}
else{
while(num2 != num1){
if(num2 % 3 == 0){
--num2;
}
if(num1 % 3 == 0){
--num1;
}
num2 /= 3;
num1 /= 3;
//Since num2Lay == num1Lay, only one of them is needed.
//--num2Lay;
--num1Lay;
//Each time the layer is decreased, we need to convert the number.
int mid = 2 * getMid(num1Lay);
num2 = mid - num2;
num1 = mid - num1;
}
return num1;
}
} public static void main(String[] args){
Scanner in = new Scanner(System.in);
int num1, num2, num1Lay, num2Lay;
while(true){
System.out.println("Input:");
num1 = in.nextInt();
num2 = in.nextInt();
num1Lay = getLayer(num1);
num2Lay = getLayer(num2);
if(num1Lay < num2Lay){
System.out.println(getCommon(num2Lay, num1Lay, num2, num1));
}
else{
System.out.println(getCommon(num1Lay, num2Lay, num1, num2));
}
System.out.println();
}
}
}
写在这里, 如果有人看到话, 希望各位能够互相交流. 肯定有更好的方法, 希望各位不吝赐教.
Amazon2014在线笔试 第三题的更多相关文章
- 2016网易春招Java在线笔试回忆录
别看是在线笔试,但是非常严格,全称窗口不得最小化和关闭,转移,全称需要打开摄像头监控,使用草稿纸需要摄像头对准……反正2个小时,题量在那儿摆着,有作弊的功夫不如好好做做最后的编程题呢……网易不让泄漏原 ...
- shanquan2的两年三题系列
好像只有2个月就退役啦 不管了,先说一下哪三题:多点求值.lcm.替罪羊树(bzoj3065) [upd0]2016.3.29 多点求值A掉啦,myy卡常数sxbk(不是说好的是shanquan2出的 ...
- Java-集合-第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; 其中,classNum 表示学生的班号,例如“class05”。 有如下List List list = new ArrayList(); l
第三题 有如下Student 对象, private String name; private int age; private int score; private String classNum; ...
- NOIP 2008提高组第三题题解by rLq
啊啊啊啊啊啊今天已经星期三了吗 那么,来一波题解吧 本题地址http://www.luogu.org/problem/show?pid=1006 传纸条 题目描述 小渊和小轩是好朋友也是同班同学,他们 ...
- NOIP2008提高组(前三题) -SilverN
此处为前三题,第四题将单独发布 火柴棒等式 题目描述 给你n根火柴棍,你可以拼出多少个形如“A+B=C”的等式?等式中的A.B.C是用火柴棍拼出的整数(若该数非零,则最高位不能是0).用火柴棍拼数字0 ...
- 《学习OpenCV》练习题第四章第三题b
#include <highgui.h> #include <cv.h> #include "opencv_libs.h" /* *<学习OpenCV ...
- 《学习OpenCV》练习题第四章第三题a
#include <highgui.h> #include <cv.h> #include "opencv_libs.h" #pragma comment ...
- 阿里聚安全攻防挑战赛第三题Android PwnMe解题思路
阿里聚安全攻防挑战赛第三题Android PwnMe解题思路 大家在聚安全挑战赛正式赛第三题中,遇到android app 远程控制的题目.我们今天带你一探究竟,如何攻破这道题目. 一.题目 购物应用 ...
- 微软2016校园招聘4月在线笔试 ABC
题目链接:http://hihocoder.com/contest/mstest2016april1/problems 第一题:输入N,P,W,H,代表有N段文字,每段有ai个字,每行有⌊W/S⌋个字 ...
随机推荐
- C++程序设计(第4版)读书笔记_指针、数组与引用
void * 函数指针和指向类成员的指针不能被赋给void * 字符串字面值常量 #include <iostream> using namespace std; void f() { c ...
- cmake工程使用distcc
distcc可以加速编译,但是遇到cmake可能就需要处理下. 问题 distcc在 /usr/lib/distcc 中放了各编译器的soft link(如cc/gcc等等),如果 /usr/lib/ ...
- 2017 Wuhan University Programming Contest (Online Round) C. Divide by Six 分析+模拟
/** 题目:C. Divide by Six 链接:https://oj.ejq.me/problem/24 题意:给定一个数,这个数位数达到1e5,可能存在前导0.问为了使这个数是6的倍数,且没有 ...
- CVS 相关内容
CVS 基础 cvs 并不是 eclipse 独有, 而是 eclipse 支持 cvs. cvs 是用于几个程序员之间共享程序避免冲突的一个工具, 是 C/S 架构. cvs 是 java 的一个开 ...
- 复制代码后调试程序,报错:在当前上下文中不存在名称“InitializeComponent”
今天在看微软的Silverlight Toolkit Samples,并对其中的项目进行MSBuild, 编译器总是报这个错误“在当前上下文中不存在名称“InitializeComponent”” 找 ...
- Spring MVC隐藏字段域
以下示例显示如何在使用Spring Web MVC框架的表单中使用隐藏字段(Hidden).首先使用Eclipse IDE来创建一个WEB工程,实现在隐藏字段中指定用户编号的功能.并按照以下步骤使用S ...
- ssl 生成证书
使用OpenSSL生成证书 1.生成RSA密钥的方法 openssl genrsa -des3 -out privkey.pem 2048 这个命令会生成一个2048位的密钥,同时有一个des3方法加 ...
- windows 32位以及64位的inline hook
Tips : 这篇文章的主题是x86及x64 windows系统下的inline hook实现部分. 32位inline hook 对于系统API的hook,windows 系统为了达成hotpatc ...
- centos7.0 安装docker
yum -y install docker docker中常用的命令 docker run -it --name 新名字 centos /bin/bash docker images 查看所有镜像 ...
- C#安装,启动,停止,卸载Windows服务
public class OptionServices { //安装服务 public static void InstallService(string filep ...