还珠格格第三部主题曲题。

  问题描述:
  算法分析:
    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
//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){
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){
private static int getMid(int layer){
if(layer & 0){
int sum = 0;
for(int i = 1; i & ++i){
sum += Math.pow(3, i);
sum += (int)((Math.pow(3, layer) + 1.0) / 2.0);
//num2Lay &= num1Lay
private static int getCommon(int num2Lay, int num1Lay, int num2, int num1){
//层数对齐
while(num1Lay != num2Lay){
if(num2 % 3 == 0){
num2 /= 3;
//Each time the layer is decreased, we need to convert the number
num2 = 2 * getMid(num2Lay) - num2;
//一起往上找父节点
if(num2 == num1){
return num1;
//final answer
while(num2 != num1){
if(num2 % 3 == 0){
if(num1 % 3 == 0){
num2 /= 3;
num1 /= 3;
//Since num2Lay == num1Lay, only one of them is needed.
//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, num2L
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));
System.out.println(getCommon(num1Lay, num2Lay, num1, num2));
System.out.println();
  写在这里, 如果有人看到话, 希望各位能够互相交流. 肯定有更好的方法, 希望各位不吝赐教.
阅读(...) 评论()第三题第三小题_百度知道
第三题第三小题
来自:作业帮
其他类似问题
为您推荐:
其他6条回答
千里之行,始于足下高谈阔论,百尺竿头,更进一步丶呕心呖血.娓娓道来
勤勤恳恳,兢兢业业严己律人,舍己为人
呕心沥血 兢兢业业
等待您来回答
下载知道APP
随时随地咨询
出门在外也不愁北航数值分析大作业第三题2014秋_图文_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
北航数值分析大作业第三题2014秋
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
下载文档到电脑,查找使用更方便
还剩16页未读,继续阅读
你可能喜欢

我要回帖

更多关于 还珠格格第三部主题曲 的文章

 

随机推荐