每日一道算法题

题目

坐标移动_牛客题霸_牛客网 (nowcoder.com)

c语言

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
//#include <string.h>

int main()
{
	char str[10001] = { 0 };
	//strcpy(str, "A10;S20;W10;D30;X;A1A;B10A11;;A10;");
	scanf("%s", str);
	char *p = str;
	int x = 0;
	int y = 0;
	int step = 0;
	char d = 0;  //方向
	while (*p != '\0')
	{
		//匹配方向
		if (!d)
		{
			if (*p== 'A' || *p == 'D' || *p == 'W' || *p == 'S')
			{
				d = *p;
				*p++;
				continue;
			}
			else
			{
				while (*p != '\0' && *p != ';')
				{
					*p++;
				}
			}
		}
		//匹配到方向
		if (d)
		{
			if (*p >= '0' && *p <= '9')
			{
				step *= 10;
				step += (*p - '0');
			}
			else if (*p == ';')
			{
				if (d == 'A') x -= step;

				else if (d == 'D') x += step;

				else if (d == 'W') y += step;

				else if (d == 'S') y -= step;

				step = 0;
				d = 0;
			}
			else
			{
				step = 0;
				d = 0;
			}
		}
		p++;

	}
	printf("%d,%d", x, y);
	return 0;
}

c++

#include <iostream>
#include <string>
#include <sstream>
#include <regex>
using namespace std;

int main()
{
	//"A10;S20;W10;D30;X;A1A;B10A11;;A10;"
	string s, c;
	//getline(cin, s);
	//cout<<s<<endl;
	while (getline(cin, s))
	{
		stringstream ss(s);
		pair<int, int> x_y(0, 0);
		while (getline(ss, c, ';'))
		{
			//cout << c << endl;
			if (c.empty()) continue;
			string _ = c.substr(1);
			//cout << _ << endl;
			if (regex_match(_, regex("[0-9]*")))
			{
				switch (c[0])
				{
				case 'A':x_y.first -= stoi(_); break;
				case 'D':x_y.first += stoi(_); break;
				case 'W':x_y.second += stoi(_); break;
				case 'S':x_y.second -= stoi(_); break;
				default:break;

				}

			}

		}
		cout << x_y.first << ',' << x_y.second;
	}
	return 0;
}

python

s=input()
s=s.split(';')
x=y=0

for _ in s:
    if not _:
        continue
    try:
        if _[0]=='A':
            x-=int(_[1:])
        elif _[0]=='D':
            x+=int(_[1:])
        elif _[0]=='W':
            y+=int(_[1:])
        elif _[0]=='S':
            y-=int(_[1:])
        else:
            continue
    except:
        continue
print(f"{x},{y}")

相关推荐

  1. 每日算法 1

    2024-06-19 07:44:01       36 阅读
  2. 每日算法 3(2023-12-11)

    2024-06-19 07:44:01       26 阅读
  3. 每日算法 14(2023-12-22)

    2024-06-19 07:44:01       28 阅读
  4. 每日编程:niven 数

    2024-06-19 07:44:01       29 阅读
  5. 每日算法day-two(备战蓝桥杯)

    2024-06-19 07:44:01       41 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-19 07:44:01       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-19 07:44:01       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-19 07:44:01       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-19 07:44:01       18 阅读

热门阅读

  1. 专业与学校:高考后的双重选择

    2024-06-19 07:44:01       8 阅读
  2. plyr音视频播放插件使用示例

    2024-06-19 07:44:01       8 阅读
  3. Distributed Systems Semester Project

    2024-06-19 07:44:01       6 阅读
  4. 标题:高考后的抉择:专业优先还是学校优先?

    2024-06-19 07:44:01       8 阅读
  5. 关于vue elementUi校验slot插槽中的表单项

    2024-06-19 07:44:01       7 阅读
  6. 编程用什么电脑不卡的:深度解析与推荐

    2024-06-19 07:44:01       7 阅读
  7. 代码随想录算法训练营第十二天

    2024-06-19 07:44:01       6 阅读
  8. CSS选择器

    2024-06-19 07:44:01       9 阅读
  9. lspci总结

    2024-06-19 07:44:01       6 阅读
  10. 实现ROS中两个里程计数据的转换到同一坐标系下

    2024-06-19 07:44:01       9 阅读