SkipStones

/*Problem Statement

When a stone is thrown across water, sometimes it will land on the water and bounce rather than falling in right away.

Suppose that a stone is thrown a distance of n. On each successive bounce it will travel half the distance as the previous bounce (rounded down to the nearest integer).

When it can not travel any further, it falls into the water.

If, at any point, the stone lands on an obstruction rather than water, it will not bounce, but will simply deflect and fall into the water.

Please look at the figure for further clarification (with black, red and green cells representing banks, obstructions and free water respectively).

So, if the stone is thrown a distance 7, it will bounce and travel a distance of 3, then finally a distance of 1,

having travelled a total distance of 11 (the green path in the figure).

If a stone is thrown a distance of 8, it will reach the opposite bank, and if thrown at distances of 2 or 6 it will hit an obstruction during its travel.

These are the three red paths in the figure.

You are given a String water. An ‘X’ represents an obstruction, while a ‘.’represents water free from obstruction.

You are to return an int representing the maximum distance a stone can travel and finally fall in the water, without hitting any obstructions,

and without reaching the opposite bank (going beyond the end of the string). You may choose any initial distance for the throw,

which starts from the left side of the string. A distance of 1 is the first character of the string, etc. If no initial throw will result in the stone

landing in the water without hitting an obstruction, return 0.


Definition:

Class:            SkipStones
Method:            maxDistance
Parameters:        String
Returns:        int
Method signature:    int maxDistance(String water)*/

package skipStones;

public class SkipStones {

    public static int maxDistance(String water) {
        int dis = initDistance(water.length());
       
        int[] distances = new int[dis];
        for(int i = 0; i < dis; i++) {
            distances[i] = i + 1;
        }
       
        int result = removeStone(distances, water);
       
        int temp = result;
        while((temp = temp/2) != 0) {
            result = result + temp;
        }
       
        return result;
    }
   
    private static int initDistance(int length) {
       
        int[] possibleTree = new int[length];
        int path = length - 1;
       
        for(int i=0; i < length; i++) {
            if(i == 0) {
                possibleTree[i] = path;
            } else {
                possibleTree[i] = possibleTree[i/2] - i - 1;
                if(possibleTree[i] == 0) return i + 1;
                else if(possibleTree[i] < 0) return i;
               
                if (i + 1 < length) {
                    possibleTree[i + 1] = possibleTree[i / 2] - i - 2;
                    if (possibleTree[i + 1] == 0) return i + 2;
                    else if (possibleTree[i + 1] < 0) return i + 1;

                    i++;
                }
            }
        }
        return 0;
    }
   
    private static int removeStone(int[] distances, String water) {
        char[] stones = water.toCharArray();
       
        for (int i = 0; i < stones.length; i++) {
            if (stones[i] == 'X') {
                if (i < distances.length) {
                    distances[i] = 0;
                }
                possibleDistance(distances, i + 1);
            }
        }
       
        for(int k = distances.length - 1; k >= 0; k--) {
            if(distances[k] != 0) return distances[k];
        }
       
        return 0;
    }
   
    private static void possibleDistance(int[] distances, int length) {
       
        int counter = 0;

        for(int i=0; i < distances.length; i++) {
            int temp = distances[i];
            if(temp == length) {
                distances[i] = 0;
            } else {
                int value = distances[i];
                while((temp = temp/2) != 0) {
                    value = value + temp;
                    if(value == length) {
                        distances[i] = 0;
                        break;
                    }
                }
            }
        }
    }
}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
旋转变压器---数字转换器作为现代伺服系统中被广泛使用的角位置测量系统,大量应用于高精度及大中型数控系统、机器人控制、工业控制、武器火力控制及惯性导航领域中。 传统的角测量系统面临的问题有:体积、重量、功耗偏大,调试、误差补偿试验复杂,费用较高。本文从微型化、智能化的方向进行研究,是解决传统角测量系统所面临问题的好途径。 本文所研究的旋转变压器---数字转换器是由信号调理模块、系统芯片C8051F064和输出控制模块组成的。整个系统的三路输入信号为X=AsinOcosar、Y=Acosθcos ot和Z=Ucosar(基准信号),输出信号为偏转角θ,输出形式为16 位数字量。信号调理模块是由模拟电路组成的,包括信号输入电路、相敏整流电路、滤波电路和直流稳压电源电路,其难点在于相敏整流电路的设计。信号调理模块的主要功能是把输入的交流信号X=AsinOcosor、Y=Acosθcosot转变成直流信号Bsinθ和Bcosθ,并使输出的直流信号在0~2.4V之间;系统芯片C8051F064是CYGNAL公司近年来推出的一款功能齐全的完全集成的混合信号片上系统型单片机。在本文所设计的系统中,系统芯片的输入信号为直流信号Bsinθ和Bcosθ,通过片内自带的2个16位A/D转换器对输入信号的数据进行采样和转换,并对转换完的数据进行滤波处理,以减小由于外界干扰而产生的误差,再用除法和反正切函数解算出偏转角θ的16位数字量;输出控制模块主要完成的功能是通过UARTO向计算机实时发送由单片机计算出来的偏转角度0的16位数字量,而串口的RS-232电平与单片机系统采用的是TTL电平之间的转换所采用的转换芯片是MC1488和MC1489。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值