题解 | #坐标移动#

坐标移动

https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29

#include <cctype>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

void splitString(vector<std::string>&vecTmp, string& s, const char& deli)
{
    auto firstPos = s.find_first_not_of(deli, 0); 
    auto lastPos = s.find_first_of(deli, firstPos);
    while(firstPos != std::string::npos || lastPos != std::string::npos)
    {
        vecTmp.push_back(s.substr(firstPos, lastPos- firstPos));
        firstPos = s.find_first_not_of(deli,lastPos);
        lastPos = s.find_first_of(deli, firstPos);
    }
}

bool IsJudgingleagalcoordinate(string& singleStr)
{
//    vector<char> vecUpper({'A','D','W','S'});
    if(singleStr.length() == 3)
    {
        if((singleStr[0] == 'A'|| singleStr[0] == 'D' 
        || singleStr[0] == 'W' || singleStr[0] == 'S')
         && isdigit(singleStr[1]) && isdigit(singleStr[2]))
        {
            return true;
        }
        return false;
    }
    if(singleStr.length() == 2)
    {
        if((singleStr[0] == 'A'|| singleStr[0] == 'D' 
        || singleStr[0] == 'W' || singleStr[0] == 'S')
         && isdigit(singleStr[1]))
        {
            return true;
        }
        return false;
    }
    return false;
}

int main() {
    int x=0, y=0;
    string str;
    vector<string> arTmp;
    string splitStr;
    getline(cin, str);
    splitString(arTmp, str, ';');
    for(int i=0;i<arTmp.size();++i)
    {
        if(IsJudgingleagalcoordinate(arTmp[i]))
        {
            splitStr = arTmp[i];
            if(splitStr[0] == 'A')
            {
                x -= atoi((splitStr.substr(1, 2)).c_str());
            }
            else if (splitStr[0] == 'D') 
            {
                x += atoi((splitStr.substr(1, 2)).c_str());
            }
            else if (splitStr[0] == 'W') 
            {
                y += atoi((splitStr.substr(1, 2)).c_str());
            }
            else 
            {
                y -= atoi((splitStr.substr(1, 2)).c_str());
            }
        }
    }
    cout << x << "," << y << endl;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

想玩飞盘的菠萝蜜在春...:上交✌🏻也拒?
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务