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

    
Class: DrawLines
Method: execute
Parameters: String[]
Returns: String[]
Method signature: String[] execute(String[] commands)
(be sure your method is public)
    

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)
    
{"FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19", "LEFT", "FORWARD 19"}
Returns: 
{"XXXXXXXXXXXXXXXXXXXX",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "X..................X",
 "XXXXXXXXXXXXXXXXXXXX" }
This sequence of commands draws a 20 x 20 outline of a square. The cursor is initially at (0, 0) pointing straight down. It then travels to (0, 19) after the first FORWARD command, painting each pixel along its path with a '*'. It then rotates 90 degrees left, travels to (19, 19), rotates 90 degrees left, travels to (19, 0), rotates 90 degrees left, and finally travels back to (0, 0).
1)
    
{"LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT", "LEFT"}
Returns: 
{"....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................." }
The cursor spins round and round, but never actually paints any pixels. The result is an empty canvas.
2)
    
{"FORWARD 1"}
Returns: 
{"X...................",
 "X...................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "....................",
 "...................." }
Going forward by one pixel creates a line that is 2 pixels long because both the start and end points are painted.
3)
    
{"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"}
Returns: 
{"XXXXXXXXXXXXXXXXXXXX",
 "...................X",
 "..XXXXXXXXXXXXXXXX.X",
 "..X..............X.X",
 "..X.XXXXXXXXXXXX.X.X",
 "..X.X..........X.X.X",
 "..X.X.XXXXXXXX.X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.X........X.X.X",
 "..X.X.XXXXXXXXXX.X.X",
 "..X.X............X.X",
 "..X.XXXXXXXXXXXXXX.X",
 "..X................X",
 "..XXXXXXXXXXXXXXXXXX",
 "...................." }

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:

using System;
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 9
One 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

    
Class: MatrixTool
Method: convert
Parameters: string
Returns: string[]
Method signature: string[] convert(string s)
(be sure your method is public)
    

Constraints

- s will contain between 1 and 50 digits, inclusive.

Examples

0)
    
"435245019"
Returns: {"435", "245", "019" }
The example above.
1)
    
"9"
Returns: {"9" }
2)
    
"0123456789"
Returns: { }
This input has 10 digits, and 10 is not a perfect square.
3)
    
"3357002966366183191503444273807479559869883303524"
Returns: {"3357002", "9663661", "8319150", "3444273", "8074795", "5986988", "3303524" }

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.

using System;
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*!= 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

    
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.

using System;

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);
    }

    


}
posted on 2005-11-23 23:24  edobnet  阅读(5261)  评论(15编辑  收藏  举报