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.
| |||||||||||||
Definition | ||||||||||||||
|
| |||||||||||||
|
| |||||||||||||
|
| |||||||||||||
Notes | ||||||||||||||
- |
Obstructions are at water level, so the stone will not hit any obstructions while it's in the air. | |||||||||||||
Constraints | ||||||||||||||
- |
water will contain between 1 and 50 elements, inclusive. | |||||||||||||
- |
Each element of water will contain between 1 and 50 characters, inclusive. | |||||||||||||
- |
Each character of each element of water will be 'X' or '.'. | |||||||||||||
Examples | ||||||||||||||
0) |
| |||||||||||||
|
| |||||||||||||
1) |
| |||||||||||||
|
| |||||||||||||
2) |
| |||||||||||||
|
| |||||||||||||
3) |
| |||||||||||||
|
| |||||||||||||
這次的題目可以說并不是太難,也許很多人被全英文的題目給難住了,其實并不應該。像我這個還沒過CET4的人都能看得懂,何況是大家呢:)好了,廢話不多說了,下面是我寫的答案:
public class SkipStones
{
public int sum;
public int total;
public int maxDistance(String water)
{
for(int i=water.length();i>0;i--)
{
total=0;
sum=0;
int j=i;
do
{
sum+=j;
j/=2;
}while(j!=0);
if(sum>water.length()) continue;
else
{
j=i;
int b=j-1;
while(j!=0)
{
if(water.charAt(b)=='X') break;
else
{
total+=j;
j/=2;
b+=j;
}
}
}
if(total==sum) break;
}
if(total==sum) return sum;
else return 0;
}
public static void main(String[] args)
{
SkipStones a=new SkipStones();
System.out.println("The maxdistance is "+a.maxDistance("..X.....X..."));
}
}