题解 | #坐标移动#

坐标移动

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

比较特别的输入字符串处理

特别好用,在需要分割处理每个小单元字符串的题 istringstream newName(originalName);

while (getline( newName, targetSring, separatingCharacter))

#include<bits/stdc++.h>
#include<bits/stdc++.h>
using namespace std;

int main()
{
    string str;
    cin >> str;
    
    // 输入重定向,这个需要记住,很好用
    istringstream s(str);
    string op;

    int x = 0, y = 0;
    
    // 输入重定向,这个需要记住,很好用
    while (getline(s, op, ';'))
    {
        int num = 0;
        if (op.size() == 2 || op.size() == 3)
        {
            // 判断是否为合法方向字符
            if (op[0] == 'A' || op[0] == 'S' || op[0] == 'D' || op[0] == 'W')
            {
                
                // 判断是否为合法 数字字符
                if (op.size() == 2)
                {
                    if (op[1] >= '0' && op[1] <= '9');
                    {
                        num = op[1] - '0';
                    }
                }
                else if (op.size() == 3)
                {
                    if (op[1] >= '0' && op[1] <= '9' && op[2] >= '0' && op[2] <= '9')
                    {
                        //cout << op << endl;
                        num = (op[1] - '0') * 10 + op[2] - '0';
                    }
                }
                else continue;
                
                // cout << op[0] << " " << num << endl;
                if (op[0] == 'A') x -= num;
                if (op[0] == 'S') y -= num;
                if (op[0] == 'D') x += num;
                if (op[0] == 'W') y += num;
            }
            else continue;
        }
    }
    cout << x << "," << y << endl;
    
    return 0;
    
}

全部评论

相关推荐

吴offer选手:学到了,下次面试也放张纸在电脑上,不然老是忘记要说哪几个点
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务