牛客[编程题]坐标移动

发布时间 2023-11-09 09:43:45作者: 张德长

 

https://www.nowcoder.com/questionTerminal/119bcca3befb405fbe58abe9c532eb29

public class Program {
    public static void Main() {
        string line;
        while ((line = System.Console.ReadLine ()) != null) { // 注意 while 处理多个 case
            string[] tokens = line.Split(";");
var point=new CPoint();
int dis;
            foreach(var token in tokens)
            {
                if (token.Length<2||token.Length>3)continue;

                if (int.TryParse(token.Substring(1),out dis))
                {
point.Move(token[0],dis);
                }


            }

            System.Console.WriteLine(point.ToString());
        }
    }
}
public class CPoint
{
private int x=0;
private int y=0;

public void Move(char dir,int dis)
{
if(dir=='A')
x-=dis;
else if(dir=='D')
x+=dis;
else if(dir=='W')
y+=dis;
else if(dir=='S')
y-=dis;
}
public override string ToString()
{
    return x.ToString()+","+y.ToString();
}
}