惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

T
Threatpost
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Engineering at Meta
Engineering at Meta
T
The Blog of Author Tim Ferriss
Recent Announcements
Recent Announcements
G
Google Developers Blog
Google DeepMind News
Google DeepMind News
The Register - Security
The Register - Security
MongoDB | Blog
MongoDB | Blog
U
Unit 42
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
L
LangChain Blog
Stack Overflow Blog
Stack Overflow Blog
P
Privacy International News Feed
L
LINUX DO - 最新话题
博客园_首页
博客园 - Franky
大猫的无限游戏
大猫的无限游戏
小众软件
小众软件
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
V
Visual Studio Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
P
Privacy & Cybersecurity Law Blog
C
Cyber Attacks, Cyber Crime and Cyber Security
K
Kaspersky official blog
C
Cisco Blogs
博客园 - 【当耐特】
阮一峰的网络日志
阮一峰的网络日志
I
Intezer
罗磊的独立博客
MyScale Blog
MyScale Blog
Last Week in AI
Last Week in AI
A
About on SuperTechFans
G
GRAHAM CLULEY
Y
Y Combinator Blog
Microsoft Security Blog
Microsoft Security Blog
GbyAI
GbyAI
T
Threat Research - Cisco Blogs
P
Proofpoint News Feed
D
DataBreaches.Net
The Hacker News
The Hacker News
Spread Privacy
Spread Privacy
AWS News Blog
AWS News Blog
I
InfoQ
T
The Exploit Database - CXSecurity.com
Simon Willison's Weblog
Simon Willison's Weblog
博客园 - 叶小钗
Project Zero
Project Zero

博客园 - edobnet

phonegap 开发初探 .net C# ADC接口中DES加密算法 阿里软件接口开发基础(淘宝网) C# SQL 2005自动备份文件删除 短信猫与中国移动CMPP2。0收发短信实例 通过OSQL命令执行SQL SERVER批SQL 针对sql 2005优化的高性能分页存储过程 .net framework 2.0以上修改config配制更容易 - edobnet LinQ操作汇总(From CSharpSamples) 远程注册表读取,与多线程池的应用. 使用JAVASCRIPT控制,IFAME的内容,从而使用投票点击等功能 ADSL自动断拨号类 类拟EXCEL表格锁定的功能的实现! - edobnet - 博客园 线程,线程数控制! sql Server 2000 分区视图的运用 利用.Net 线程池提高应用程序性能. 触发器在增量同步数据的运用. 进程服务编写,与启动停止控制 js异步XMLhttpPost,并带有,等待显示,防正,XMLhttpPost请求时间过来,而使浏览器死掉! - edobnet - 博客园
google 中国编程大赛测试题,及自己的解答
edobnet · 2005-11-23 · via 博客园 - edobnet

网站: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)
    
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)
    
Returns: {"435", "245", "019" }
1)
    
2)
    
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)
    
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)
    
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);
    }

    


}