题解 | #坐标移动#

坐标移动

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

// HJ17-3 坐标移动.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include<iostream>
#include<bits/stdc++.h>

using namespace std;

int a = 0, b = 0;

void check(string& str)
{
	if (str == " ")
	{
		return;
	}
	if (str[0] == 'A' || str[0] == 'D')
	{
		int num = 0;
		if (str[0] == 'A')
		{
			for (int i = 1; i < str.size(); i++)
			{
				if (isdigit(str[i]))
				{
					num = num * 10 + str[i] - '0';
				}
				else
				{
					return;
				}
			}
			a -= num;
		}
		if (str[0] == 'D')
		{
			for (int i = 1; i < str.size(); i++)
			{
				if (isdigit(str[i]))
				{
					num = num * 10 + str[i] - '0';
				}
				else
				{
					return;
				}
			}
			a += num;
		}
	}
	if (str[0] == 'W' || str[0] == 'S')
	{
		int num = 0;
		if (str[0] == 'W')
		{
			for (int i = 1; i < str.size(); i++)
			{
				if (isdigit(str[i]))
				{
					num = num * 10 + str[i] - '0';
				}
				else
				{
					return;
				}
			}
			b += num;
		}
		if (str[0] == 'S')
		{
			for (int i = 1; i < str.size(); i++)
			{
				if (isdigit(str[i]))
				{
					num = num * 10 + str[i] - '0';
				}
				else
				{
					return;
				}
			}
			b -= num;
		}
	}
}

int main()
{
	string s,str;
	while (getline(cin, s))
	{
		a = 0, b = 0;
		stringstream ss(s);
		while (getline(ss, str, ';'))
		{
			check(str);
		}
		cout << a << "," << b << endl;
	}
	return 0;
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

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