坐标变换,难点在过滤无效坐标和对输入的处理
坐标移动
http://www.nowcoder.com/questionTerminal/119bcca3befb405fbe58abe9c532eb29
#include <iostream> #include <string> #include <vector> #include <sstream> using namespace std; int main() { string ss; while(getline(cin,ss)) //多组数据 { int ssLen = ss.size(); for(int i = 0;i < ssLen;i++) //将;换成空格,方便分解 { if(ss[i] == ';') ss[i] = ' '; } stringstream ss1(ss); string str; vector<char> c; vector<int> num; while(ss1 >> str) { int strLen = str.size(); if(strLen >= 2 && strLen <= 3) //由题可知,有效字符串长度范围为:2~3 { if(str[0] == 'A' || str[0] == 'S' || str[0] == 'W' || str[0] == 'D') //由题可知,有效字符串首字符应为:A,S,W,D { bool flag = true; for(int j = 1;j < strLen;j++) //有效字符串首字符后应该跟数字字符 { if(!(str[j] >= '0' && str[j] <= '9')) { flag= false; break; } } if(!flag) continue; c.push_back(str[0]); //首字符后全为数字字符时,即这是一个合法的坐标变换时,才保存方向 int sum = 0; for(int j = 1;j < strLen;j++) { sum *= 10; sum += (str[j] - '0'); } num.push_back(sum); //合法的坐标变换时,保存数字 } } } int x= 0; int y= 0; int count = c.size(); for(int k = 0;k < count;k++) { switch(c[k]) { case 'A': x -= num[k];break; case 'D': x += num[k];break; case 'S': y -= num[k];break; case 'W': y += num[k];break; } } cout << x << ',' << y << endl; } return 0; }