题解 | #坐标移动#C++ 简单明了,耐心一些即可

坐标移动

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

#include<vector>
#include<string>
#include<sstream>
using namespace std;
int main()
{
	//首先第一步要读取这一行字符串中分隔隔开的每一个坐标点
	//第一步:
	//读入一整行字符串
	//当读入字符不为;时压入临时字符串
	//读到;的时候再把临时字符串压入字符串数组
	
	string input;
	getline(cin, input);
	vector<string>move;

	for (int i = 0; i < input.size(); i=i+1)
	{
		string temp;
		while (input[i] != ';')
		{
			temp.push_back(input[i]);
			i = i + 1;
		}
		move.push_back(temp);
	}
	//cout << move[1] << endl;
	int x=0, y = 0;
	for (int i = 0; i < move.size(); i++)
	{
		if (move[i].size() == 3||move[i].size()==2)
		{
			if (move[i][0] == 'A' || move[i][0] == 'D' || move[i][0] == 'W' || move[i][0] == 'S')
			{
				int length1 = move[i][1] - '0';
				if (move[i].size() == 2 && length1 >= 0 && length1 <= 9)
				{
					if (move[i][0] == 'A')
					{
						x = x - length1;
					}
					if (move[i][0] == 'D')
					{
						x = x + length1;
					}
					if (move[i][0] == 'W')
					{
						y = y +length1;
					}
					if (move[i][0] == 'S')
					{
						y = y - length1;
					}
				}
				else
				{
					int length2 = move[i][2] - '0';
					if (length1 >= 0 && length1 <= 9 && length2 >= 0 && length2 <= 9)
					{
						int length = 10 * length1 + length2;
						if (move[i][0] == 'A')
						{
							x = x - length;
						}
						if (move[i][0] == 'D')
						{
							x = x + length;
						}
						if (move[i][0] == 'W')
						{
							y = y + length;
						}
						if (move[i][0] == 'S')
						{
							y = y - length;
						}
					}
				}
			}
		}
	}
	cout << x << "," << y << endl;
	return 0;

}
全部评论
move[i][0] == 'A' || move[i][0] == 'D' || move[i][0] == 'W' || move[i][0] == 'S'),这个二维怎末理解
点赞 回复 分享
发布于 2022-05-04 14:23

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务