题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;
int main() {
string str;
cin>>str;
int x=0, y=0;
while (true) {
if (str.length()<2) {
break;
}
auto index = str.find(";");
if (string::npos == index) {
break;
}
string tmp=str.substr(0, index);
str=str.substr(index+1);
int move=0;
if (tmp.length()==2) {
move = tmp[1]-'0';
move = move>10 ? 0:move;
}
else if (tmp.length()==3) {
if (tmp[1]-'0' >10 || tmp[2]-'0'>10) {
continue;
}
move = tmp[2]-'0' + 10*(tmp[1]-'0');
}
switch (tmp[0]) {
case 'A':
x-=move;
break;
case 'D':
x+=move;
break;
case 'W':
y+=move;
break;
case 'S':
y-=move;
break;
}
}
printf("%d,%d", x,y);
}
// 64 位输出请用 printf("%lld")
主要是对输入的校验
查看14道真题和解析
