题解 | 坐标移动

坐标移动

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

#include <iostream>
using namespace std;

bool is_num(string s) {
    for (char c : s) {
        if ( c < '0' || c > '9') {
            return false;
        }
    }
    return true;
}


int main() {
    string str;
    pair<int, int>p(0, 0);
    while (getline(cin, str, ';')) {
        if (str.empty()) continue;
        string s1 = str.substr(1);
        if (is_num(s1)) {
            switch (str[0]) {
                case 'A':
                    p.first -= stoi(s1);
                    break;
                case 'D':
                    p.first += stoi(s1);
                    break;
                case 'W':
                    p.second += stoi(s1);
                    break;
                case 'S':
                    p.second -= stoi(s1);
                    break;
                default:
                    break;
            }
        }
    }
    cout << p.first <<"," <<  p.second;
    return 0;
}
// 64 位输出请用 printf("%lld")

getline(cin, str, ';') 这个函数能够自定义终止符,同时stoi和aiot这两个函数负责字符和数字之间的转换;

全部评论

相关推荐

asdasdasda...:19岁,不容易啊可能升个本会好点,现在学历歧视太严重了
点赞 评论 收藏
分享
06-10 21:15
门头沟学院 Java
宁阿:好多这种没🧠的公司,他们估计都不知道毕业的人不能给安排实习岗
实习吐槽大会
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-07 11:35
程序员小白条:话太多,没实力和学历,差不多回答回答就行了,身份地位不一样
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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