爱上google
介绍和GOOGLE相关的资讯。
-
2005-11-28
GOOGLE挑战赛练习题3及答案 - [google相关]
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://topcc.52blog.net/logs/2878260.html
Problem Statement
When editing a single line of text, there are four keys that can be used to move the cursor: end, home, left-arrow and right-arrow. As you would expect, left-arrow and right-arrow move the cursor one character left or one character right, unless the cursor is at the beginning of the line or the end of the line, respectively, in which case the keystrokes do nothing (the cursor does not wrap to the previous or next line). The home key moves the cursor to the beginning of the line, and the end key moves the cursor to the end of the line.
You will be given a int, N, representing the number of character in a line of text. The cursor is always between two adjacent characters, at the beginning of the line, or at the end of the line. It starts before the first character, at position 0. The position after the last character on the line is position N. You should simulate a series of keystrokes and return the final position of the cursor. You will be given a String where characters of the String represent the keystrokes made, in order. 'L' and 'R' represent left and right, while 'H' and 'E' represent home and end.Definition
Class: CursorPosition Method: getPosition Parameters: String, int Returns: int Method signature: int getPosition(String keystrokes, int N) (be sure your method is public) Constraints
- keystrokes will be contain between 1 and 50 'L', 'R', 'H', and 'E' characters, inclusive. - N will be between 1 and 100, inclusive. Examples
0) "ERLLL"
10
Returns: 7
First, we go to the end of the line at position 10. Then, the right-arrow does nothing because we are already at the end of the line. Finally, three left-arrows brings us to position 7. 1) "EHHEEHLLLLRRRRRR"
2
Returns: 2
All the right-arrows at the end ensure that we end up at the end of the line. 2) "ELLLELLRRRRLRLRLLLRLLLRLLLLRLLRRRL"
10
Returns: 3
3) "RRLEERLLLLRLLRLRRRLRLRLRLRLLLLL"
19
Returns: 12
This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
答案:1
2
public class CursorPosition
{
3
public int getPosition(String keystrokes, int N)
{
4
int position = 0;
5
String s = "";
6
for(int i = 0; i < keystrokes.length(); i++)
{
7
s = keystrokes.substring(i, i+1);
8
if("L".equals(s))
{
9
if(position == 0) continue;
10
position--;
11
}
12
if("R".equals(s))
{
13
if(position == N) continue;
14
position++;
15
}
16
if("H".equals(s))
{
17
position = 0;
18
}
19
if("E".equals(s))
{
20
position = N;
21
}
22
23
}
24
25
return position;
26
27
}
28
/** *//**
29
* @param args
30
*/
31
public static void main(String[] args)
{
32
CursorPosition cursorPosition = new CursorPosition();
33
int cursor = cursorPosition.getPosition("ERLLL", 10);
34
System.out.println("cursor:" + cursor);
35
}
36
37
}随机文章:
英特尔Mac版Firefox3月亮相 尚有问题需解决 2006-01-17何为圣诞节? 2005-12-23Google Talk 今天升级到1.0.0.78了,更新呀。 2005-12-01GOOGLE挑战赛练习题1及答案 2005-11-28Google Talk的使用(详解) 2005-11-24
收藏到:Del.icio.us





