HJ17 坐标移动

在这里插入图片描述
在这里插入图片描述

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

//分割字符串
vector<string> split(string str, const char c){
	vector<string> sstr;
	int pos = 0;
	while (pos < str.length()){
		int end;
		for (end = pos; str[end] != c && end < str.length(); end++);
		sstr.push_back(str.substr(pos, end - pos));
		pos = end + 1;
	}
	return sstr;
}

//判断合法性
bool judge(string str){
    int count = 0;
    for (int i = 0; i < str.size(); ++i) {
        if (str[i] == 'A' || str[i] == 'S' || str[i] == 'D' || str[i] == 'W'){
            count++;
        }
    }
	return count == 1;
}

int main(){
	string inputs;
	cin >> inputs;
    int x = 0;
    int y = 0;
    vector<string> allStr = split(inputs, ';');
    for (auto it = allStr.begin(); it != allStr.end(); it++){
        if (judge(*it)){
            //截取数字,并将string转换成c字符串,然后将c字符串用atoi转换成数字
            int num = atoi(((*it).substr(1)).c_str());
            switch ((*it)[0])
            {
            case 'A':
                x -= num; break;
            case 'S':
                y -= num; break;
            case 'W':
                y += num; break;
            case 'D':
                x += num; break;
            }
        }
    }
    cout << x << "," << y << endl;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值