google中国编程挑战赛-练习题之一(1000分题)

qwerttyy 2005-12-08 01:32:14
全英文滴~看不太懂啊~~~~55555555555555

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.
...全文
448 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
sarrow 2005-12-10
  • 打赏
  • 举报
回复
看不懂。
qwerttyy 2005-12-10
  • 打赏
  • 举报
回复
up
sanjie88 2005-12-10
  • 打赏
  • 举报
回复
题目好像是从网页上拉下来的,
thunderclap 2005-12-09
  • 打赏
  • 举报
回复
强烈要求weisunding(鼎鼎)发代码。
qwerttyy 2005-12-09
  • 打赏
  • 举报
回复
weisunding(鼎鼎)

你牛啊。

有题目和你做的答题吗?发上来看看吧。
fokker 2005-12-09
  • 打赏
  • 举报
回复
using System;
using System.Collections;
public class CursorPosition
{
public int getPosition(string keystrokes, int N)
{
Stack st = new Stack();
int iPos = 1;
for( int i=keystrokes.Length-1; i>=0; i-- )
{
if( keystrokes[i] != 'E' && keystrokes[i] != 'H' )
{
st.Push( keystrokes[i] );
}
else
{
if( keystrokes[i] == 'E' ) iPos = N;
break;
}
}
while( st.Count > 0 )
{
char c = (char)st.Pop();
iPos = addPos( iPos, c, N );
}
return iPos;
}

private int addPos( int iPos, char cOp, int N )
{
if( cOp == 'L' )
{
if( iPos == 1 ) return 1;
iPos--;
}
else
{
if( iPos == N ) return N;
iPos++;
}
return iPos;
}

}
qwerttyy 2005-12-09
  • 打赏
  • 举报
回复
得分:878.6

using System;

public class CursorPosition
{
public int getPosition(string keystrokes, int N)
{
int iPos = 0;
char[] cArray = keystrokes.ToCharArray();

for (int i = 0; i < keystrokes.Length; i++)
{
switch (cArray[i])
{
case 'E':
iPos = N;
break;
case 'H':
iPos = 0;
break;
case 'L':
iPos--;
break;
case 'R':
iPos++;
break;
}
if (iPos > N) iPos = N;
else if (iPos < 0) iPos = 0;
}
return iPos;
}
}

feiyun0112 2005-12-08
  • 打赏
  • 举报
回复
假设有一个字符串,长度为N,keystrokes是按的键,用来移动光标


要得到光标最后在哪个位置
leonchenjian 2005-12-08
  • 打赏
  • 举报
回复
比较简单的一题,1000分的难道是第三题?
TechEye 2005-12-08
  • 打赏
  • 举报
回复
我得了997分 :)
jinjazz 2005-12-08
  • 打赏
  • 举报
回复
题目都看不懂就不要参和勒

110,537

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧