题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <bits/stdc++.h> #include <string> using namespace std; int dis = 0; int strvalid(string str){ int cifang = 1; if(str[0] == 'W' || str[0] == 'A' || str[0] == 'S' || str[0] == 'D'){ for(int i = str.size() - 1; i > 0; i--){ if(str[i] > '9' || str[i] < '0') { dis = 0; return 0; } else{ dis += (str[i] - '0')*cifang; cifang *= 10; } } return 1; } return 0; } int main() { int x = 0; int y = 0; string str; while(getline(cin, str,';')){ dis = 0; // if(cin.get() == '\n') break; if(strvalid(str)){ switch(str[0]){ case 'W' : y += dis; break; case 'A' : x -= dis; break; case 'S' : y -= dis; break; case 'D' : x += dis; break; } } } cout << x << ',' << y << endl; } // 64 位输出请用 printf("%lld")