华为机试-坐标移动

来源:互联网 发布:windows更新卡住了 编辑:程序博客网 时间:2024/06/14 03:18
#include<iostream>#include<string>#include<math.h>#include<vector>#include<map>#include<set>using namespace std;typedef struct point{    int posx;    int posy;}pos;void SplitSting(const string& strInput, vector<string> &vec, const string& c){    string::size_type pos1 = 0;    string::size_type pos2 = strInput.find(c);    while(string::npos != pos2)    {        vec.push_back(strInput.substr(pos1, pos2-pos1));        pos1 = pos2 + c.size();        pos2 = strInput.find(c, pos1);    }}int main(){    string input;     while(cin>>input)    {        pos pointer = {0,0};        vector<string> vecInput;        SplitSting(input, vecInput, ";");        for(int i = 0; i<vecInput.size(); i++)        {            bool isInvalid = false;            int sum=0;            if(vecInput[i].size()>1 && vecInput[i].size()<=3)            {                for(int j = 1; j <=vecInput[i].size() - 1; j++)                {                    if(vecInput[i][j]>='0' && vecInput[i][j]<='9')                        sum = sum*10 +(vecInput[i][j] - '0');                    else                    {                        isInvalid = true;                        break;                                      }                }                if(!isInvalid)                {                    switch(vecInput[i][0])                    {                        case 'A':                            pointer.posx -= sum;                            break;                        case 'S':                            pointer.posy -= sum;                            break;                        case 'W':                            pointer.posy += sum;                            break;                        case 'D':                            pointer.posx += sum;                            break;                        default:                            break;                    }                }            }        }        cout<<pointer.posx<<','<<pointer.posy<<endl;    }    return 0;}
原创粉丝点击