题解 | #坐标移动#

坐标移动

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

1.输入
vector<vector<char>>装所有输入;</char>

2.
输入时判断输入字符串是否合法
检查合法项分别为首字母、总字符个数、第二个字符和第三个字符
不要忘了不管字符是否合法,遇到分号后要清空v1

3.代码
#include<iostream>
#include</iostream>

using namespace std;

int is_hefa(vector<char> s)
{
if(s[0]!='A'&&s[0]!='D'&&s[0]!='W'&&s[0]!='S')
return 0;</char>

if(s.size()>4 || s.size()<=1)
    return 0;
if(s.size() >= 3)
{
    if(s[1]<'0' || s[1]>'9')
        return 0;
    if(s.size() == 4)
    {
        if(s[2]<'0'||s[2]>'9')
            return 0;
    }
}
return 1;

}

int main()
{
char c;
vector<char> v1;
vector<vector<char>> v2;
while(c = getchar())
{
v1.push_back(c);
if(c == ';')
{
if(is_hefa(v1))
{
v1.pop_back();
v2.push_back(v1);
}
v1.clear();
}
if(c == '\n')
break;
}
int pos[2] = {0,0};
int x;
for(int i = 0;i<v2.size();i++)
{
v1 = v2[i];
if(v1.size() == 3)
x = (v1[1]-'0')*10+v1[2]-'0';
if(v1.size() == 2)
x = v1[1]-'0';
if(v1[0] == 'A')
{
pos[0] = pos[0]-x;
}
if(v1[0] == 'S')
{
pos[1] = pos[1]-x;
}
if(v1[0] == 'W')
{
pos[1] = pos[1]+x;
}
if(v1[0] == 'D')
{
pos[0] = pos[0]+x;
}
}</char></char>

cout<<pos[0]<<","<<pos[1]<<endl;

}

全部评论

相关推荐

11-04 10:30
已编辑
门头沟学院 研发工程师
开心小狗🐶:“直接说答案”
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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