题解 | #坐标移动#
坐标移动
https://www.nowcoder.com/practice/119bcca3befb405fbe58abe9c532eb29
// HJ17 坐标移动.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include<iostream>
#include<bits/stdc++.h>
using namespace std;
bool isNum(string s)
{
int ans = 0;
for (int i = 0; i < s.size(); i++)
{
if (isdigit(s[i]))
ans++;
}
if (ans == s.size()&&ans!=0)
return true;
else
return false;
}
int main()
{
pair<int, int>dp(0,0);
string s;
while (getline(cin, s, ';'))
{
if (s.empty())
continue;
string s1 = s.substr(1);
if (isupper(s[0]) && isNum(s1)&&s1.size()<=2)
{
switch (s[0])
{
case 'A':dp.first -= stoi(s1); break;
case 'D':dp.first += stoi(s1); break;
case 'W':dp.second += stoi(s1); break;
case 'S':dp.second -= stoi(s1); break;
}
}
}
cout << dp.first << ',' << dp.second << endl;
return 0;
}
查看1道真题和解析