题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
bool isNum(string& str) {
for (auto s : str) {
if (!isdigit(s)) return false;
}
return true;
}
int main() {
pair<int,int> pos(0,0);
string str;
while (getline(cin, str, ';')) {
if(str.empty()) continue;
string nstr = str.substr(1);
if(isNum(nstr) && nstr.length() <=2) {
switch(str[0]) {
case 'A':
pos.first -= stoi(nstr);
break;
case 'D':
pos.first += stoi(nstr);
break;
case 'W':
pos.second += stoi(nstr);
break;
case 'S':
pos.second -= stoi(nstr);
break;
default:
break;
}
}
}
cout << pos.first << "," << pos.second << endl;
}
// 64 位输出请用 printf("%lld")
查看3道真题和解析
深信服公司福利 896人发布