【华为OJ】【016-坐标移动】
题目链接:
https://blog.csdn.net/DERRANTCM/article/details/51328464
C++实现:
void main()
{
string str1,strOut;
vector<string> strList,strValid;
cin >> str1;
str1 = "A10;S20;W10;D30;X;A1A;B10A11;;A10;";
//分割字符串
string::size_type pos1 = 0,pos2;
pos2 = str1.find(";");
while(pos2 != str1.npos)
{
strList.push_back(str1.substr(pos1,pos2-pos1));
pos1 = pos2 + 1;
pos2 = str1.find(";",pos1);
}
regex r("^[AWSD]([0-9]{1,2})$");
char dir;
int x1=0,y1=0,n1,n2;
stringstream ss;
for (int i=0;i<strList.size();i++)
{
if (regex_match(strList[i],r))
{
strValid.push_back(strList[i]);
dir = strList[i].at(0);
string strTem = strList[i].substr(1);
ss.clear();//不清空转换结果会一直是第一次的
ss << strTem;
ss >> n1;
switch(dir)
{
case 'W':
y1 += n1;
break;
case 'S':
y1 -= n1;
break;
case 'A':
x1 -= n1;
break;
case 'D':
x1 += n1;
break;
}
}
}
cout << x1 << "," << y1 << endl;
system("pause");
}