题解 | #坐标移动#

坐标移动

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

line = input('', 's');
lines = strsplit(line, ';');
n = length(lines);
axis = [0, 0];
for i = 1:n
    move = lines(i);
    move = cell2mat(move);
    if length(move) == 3 || length(move) == 2
        if isstrprop(move(2), 'digit')&&isstrprop(move(end), 'digit')
            l = str2num(move(2:end));
            switch move(1)
                case('A')
                    axis(1) = axis(1)-l;
                case('S')
                    axis(2) = axis(2)-l;
                case('W')
                    axis(2) = axis(2)+l;
                case('D')
                    axis(1) = axis(1)+l;
                otherwise
                    continue
            end
        else 
            continue
        end
    else 
        continue
    end
end
fprintf('%d,%d', axis(1), axis(2))

全部评论

相关推荐

helloWord大王:这时候hr来个转人工我就真绷不住了
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务