google 中国编程大赛测试题,及自己的解答
网站:http://www.topcoder.com/pl/?module=Static&d1=gccj05&d2=ZH_rules
题目1:
Problem Statement | |||||||||||||
A simple line drawing program uses a blank 20 x 20 pixel canvas and a directional cursor that starts at the upper left corner pointing straight down. The upper left corner of the canvas is at (0, 0) and the lower right corner is at (19, 19). You are given a String[], commands, each element of which contains one of two possible commands. A command of the form "FORWARD x" means that the cursor should move forward by x pixels. Each pixel on its path, including the start and end points, is painted black. The only other command is "LEFT", which means that the cursor should change its direction by 90 degrees counterclockwise. So, if the cursor is initially pointing straight down and it receives a single "LEFT" command, it will end up pointing straight to the right. Execute all the commands in order and return the resulting 20 x 20 pixel canvas as a String[] where character j of element i represents the pixel at (i, j). Black pixels should be represented as uppercase 'X' characters and blank pixels should be represented as '.' characters. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Notes | |||||||||||||
- | The cursor only paints the canvas if it moves (see example 1). | ||||||||||||
Constraints | |||||||||||||
- | commands will contain between 1 and 50 elements, inclusive. | ||||||||||||
- | Each element of commands will be formatted as either "LEFT" or "FORWARD x" (quotes for clarity only), where x is an integer between 1 and 19, inclusive, with no extra leading zeros. | ||||||||||||
- | When executing the commands in order, the cursor will never leave the 20 x 20 pixel canvas. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|
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:
public class DrawLines
{
int x = 0; //now x
int y = 0; //now y
int d = 1; //direction
//data
string[,] cav = new string [20,20];
public string[] execute(string[] commands)
{
//Console.WriteLine("Hello World");
//init data
for(int i =0 ;i<20;i++)
{
for(int n = 0;n<20;n++)
{
cav[i,n] = ".";
}
}
//do command
foreach(string command in commands)
{
doCommand(command);
}
string[] ret = new string[20];
for(int i=0;i<20;i++)
{
string s = "";
for(int n=0;n<20;n++)
{
s+= cav[i,n];
}
ret[i] = s;
}
return ret;
}
public void doCommand( string command)
{
command = command.Trim();
if(command == "LEFT")
{
ChangeDirection( );
return;
}
string[] sp = command.Split(' ');
if(sp.Length != 2)
Exception("error"+command);
DoForword(int.Parse(sp[1]));
}
public void DoForword(int step)
{
int end = 0;
switch(d)
{
case 0:
end = x + step;
if(end > 19)
{
Exception("Out line");
}
for(int i = x;i<= end;i++)
{
cav[y,i] = "X";
}
x = end;
break;
case 1:
end = y + step;
if(end > 19)
{
Exception("Out line");
}
for(int i = y;i<=end;i++)
{
cav[i,x] = "X";
}
y = end;
break;
case 2:
end = x - step;
if(end < 0)
{
Exception("Out line");
}
for(int i = end;i<=x;i++)
{
cav[y,i] = "X";
}
x = end;
break;
case 3:
end = y - step;
if(end < 0)
{
Exception("Out line");
}
for(int i = end;i<=y;i++)
{
cav[i,x] = "X";
}
y = end;
break;
default:
break;
}
}
public void Exception(string name)
{
throw new Exception(name);
}
/// <summary>
/// ChangeDirection
/// </summary>
public void ChangeDirection()
{
d = d -1 ;
if(d<0)
d = 3;
}
public static void Main(string[] args)
{
// string[] d = new string[]{"FORWARD 1"};
string[] d = new string[]{"LEFT", "FORWARD 19", "LEFT", "LEFT", "LEFT",
"FORWARD 18", "LEFT", "LEFT", "LEFT", "FORWARD 17",
"LEFT", "LEFT", "LEFT", "FORWARD 16", "LEFT",
"LEFT", "LEFT", "FORWARD 15", "LEFT", "LEFT", "LEFT",
"FORWARD 14", "LEFT", "LEFT", "LEFT", "FORWARD 13",
"LEFT", "LEFT", "LEFT", "FORWARD 12", "LEFT", "LEFT",
"LEFT", "FORWARD 11", "LEFT", "LEFT", "LEFT", "FORWARD 10",
"LEFT", "LEFT", "LEFT", "FORWARD 9", "LEFT", "LEFT",
"LEFT", "FORWARD 8", "LEFT", "LEFT", "LEFT", "FORWARD 7"};
// string[] d = new string[]{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"};
DrawLines drawline = new DrawLines();
string[] ret = drawline.execute(d);
foreach(string s in ret)
{
Console.WriteLine(s);
}
Console.ReadLine();
}
}
题目2:
Problem Statement | |||||||||||||
A square matrix is a grid of NxN numbers. For example, the following is a 3x3 matrix: 4 3 5 2 4 5 0 1 9One way to represent a matrix of numbers, each of which is between 0 and 9 inclusive, is as a row-major string. To generate the string, simply concatenate all of the elements from the first row followed by the second row and so on, without any spaces. For example, the above matrix would be represented as "435245019". You will be given a square matrix as a row-major string. Your task is to convert it into a string[], where each element represents one row of the original matrix. Element i of the string[] represents row i of the matrix. You should not include any spaces in your return. Hence, for the above string, you would return {"435","245","019"}. If the input does not represent a square matrix because the number of characters is not a perfect square, return an empty string[], {}. | |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Constraints | |||||||||||||
- | s will contain between 1 and 50 digits, inclusive. | ||||||||||||
Examples | |||||||||||||
0) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|
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.
public class MatrixTool
{
public String[] convert(String s)
{
int l = s.Length;
if(l>50)
throw new Exception("param s error");
int t = (int)Math.Sqrt(l);
if(t*t != l)
return new string[]{};
string[] ret = new string[t];
for(int i = 0;i< t;i++)
{
ret[i] = s.Substring(i*t,t);
}
return ret;
}
}
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 | |||||||||||||
| |||||||||||||
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) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
|
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.
public class CursorPosition
{
public int getPosition(String keystrokes, int N)
{
int p = 0;
if(N > 100 || N < 1)
{
DoExcetpion("Param N is Must Between 0-100");
}
if(keystrokes.Length >50)
{
DoExcetpion("Param keystrokes length Must less 50");
}
int[] key = new int[N];
for(int i = 0;i< N; i++)
{
key[i] = i+1;
}
foreach(char x in keystrokes.ToCharArray())
{
switch(x)
{
case 'L':
if(p != 0)
p --;
break;
case 'R':
if(p != N-1)
p++;
break;
case 'H':
p= 0;
break;
case 'E':
p = N-1;
break;
default:
DoExcetpion("Error keystrokes");
break;
}
}
return key[p];
}
private void DoExcetpion(string name)
{
throw new Exception(name);
}
}