題解 | 坐標(biāo)移動
坐標(biāo)移動
http://www.fangfengwang8.cn/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> using namespace std; bool is_num(string s) { for (char c : s) { if ( c < '0' || c > '9') { return false; } } return true; } int main() { string str; pair<int, int>p(0, 0); while (getline(cin, str, ';')) { if (str.empty()) continue; string s1 = str.substr(1); if (is_num(s1)) { switch (str[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")
getline(cin, str, ';') 這個函數(shù)能夠自定義終止符,同時stoi和aiot這兩個函數(shù)負(fù)責(zé)字符和數(shù)字之間的轉(zhuǎn)換;