题解 | #坐标移动#
坐标移动
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))
查看5道真题和解析