题解 | #坐标移动#__huawei-no.17
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <string> using namespace std; bool isNum(string s) { int len = s.size(); for (int i = 0 ; i < len; i++) { if ('0' <= s[i] && s[i] <= '9') continue; else return false; } return true; } int main() { string s; pair<int, int>p(0, 0); while (getline(cin, s, ';')) { if (s.empty()) continue; string s1 = s.substr(1); //提取第一个字符之后的 if (isNum(s1)) { switch (s[0]) { case 'A': p.first -= stoi(s1); break; case 'D': p.first += stoi(s1); break; case 'W': p.second += stoi(s1); break; case 'S': p.second -= stoi(s1); break; default: break; } } } cout<<p.first<<","<<p.second; return 0; } // 64 位输出请用 printf("%lld")
1、首先,利用getline可以指定分隔符进行读取
2、如何判断数字有效,共同点都在第一个字母之后的数字,所以要substr
3、使用了一个哈希表的东西?pair我不是很熟悉他的使用方法,后面一查才知道这是模板类,还有模板函数等等