YACS(上海计算机学会竞赛平台)2023年12月月赛——移动复位

移动复位

内存限制: 256 Mb时间限制: 1000 ms

题目描述

二维平面上有一个点。该点最初所在的位置称之为起点。接下来,该点接受了一串命令,每个命令可以用一个大写字母表示:

  • R 表示该点沿 X 轴坐标正方向移动了一个单位;
  • L 表示该点沿 X 轴坐标负方向移动了一个单位;
  • U 表示该点沿 Y 轴坐标正方向移动了一个单位;
  • D 表示该点沿 Y 轴坐标负方向移动了一个单位。

执行完这些指令后,该点不一定回到起点。请计算至少需要增加多少条指令,才能让这个点回到起点?如果该点已经到达起点,则输出 0。

输入格式
  • 单个字符串:表示给定的指令序列
输出格式
  • 单个整数:表示最少还需添加最少指令才能回到起点。
数据范围
  • 字符数量不超过 100000
样例数据
输入1:

LRUD

输出1:

0

输入2:

LLUU

输出2:

4

思考过程

可以运用初中的知识:

1.平面直角坐标系相当于两个互相垂直的数轴

2.绝对值可以表示距离(表示方法:|0-x|

——————————————————————————

L相当于x-1,R相当于x+1,U相当于y+1,D相当于y-1

操作完上下左右后,最后的答案就是\left | 0-x \right |+\left | 0-y \right |

代码实现
#include <bits/stdc++.h>
using namespace std;
int x, y;

int main() {
	string s;
	cin >> s;
	for (int i = 0; i < s.length(); i++) {
		if (s[i] == 'R')
			x++;
		if (s[i] == 'L')
			x--;
		if (s[i] == 'U')
			y++;
		if (s[i] == 'D')
			y--;
	}
	cout << abs(0 - x) + abs(0 - y);
	return 0;
}

  • 9
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
YACS 是一个强大的 PHP 脚本,可以让你维护一个动态的 Web 服务器。特性:- Runs on your own server, or on a shared web site- Post articles with web forms, by e-mail, or remotely (w:bloggar)- Embed images and photos in articles --automatic resize- Each section can be a weblog, a discussion board, a book of cooking recipes,etc, or even a plain list of articles- Overlay interface for PHP developers, to add extra functionality to articles,such as polls or cooking recipes- Display the content tree in Freemind- Comments, with quoting- Archives per week and per month- The home page is updated automatically on article publishing- Categories, sub-categories, etc. --Build your own Yahoo! or DMOZ...- Real-time meetings with community members- Private discussions and messages- Search on any word --text of articles is fully indexed- Multiple authors --actually, a community of contributors- Articles are visible only on publication after review by editors- Articles and sections can have dead-line to limit visibility over time- A straightforward control panel, and a set of configuration panels- File upload to articles , sections or categories- Attach links to articles, sections or categories- A comprehensive set of UBB-like codes are available to beautify your posts- Integrated support of TinyMCE and of FCKEditor- Fully customizable skins- Easy integration of Google Maps- Add a comprehensive web interface to existing collections of files- Support audio-on demand and video-on demand- Automatic web slideshow for shared photos- RSS syndication- Easy installation- XML-RPC interface (implementing the Blogger API and metaWeblog API) 标签:YACS
上海市青少算法竞赛YACS)是上海市面向青少举办的一项竞赛活动。这个竞赛旨在提供给青少一个展示和提升他们算法能力的平台YACS竞赛设有多个不同级别的组别,包括初级、中级和高级组。每个组别的参者按照一定的选拔方式进行参,确保参者能够在相对适合的级别中进行竞争。竞赛内容包括各类算法问题的解题,包括搜索、排序、图论等等,涵盖了计算机科学中的基础知识和技巧。参者在规定的时间内解答尽可能多的问题,并根据正确性、效率和创新性等多个维度进行评分。 YACS竞赛不仅提供了一个交流和比拼的平台,还对青少算法能力的发展有着积极的推动作用。通过这个竞赛,学生们可以在实践中加深对算法的理解,并且学习如何将算法应用于解决实际问题。而且,竞赛中的参经验也可以在日后的学习和工作中发挥重要的作用。 此外,YACS竞赛还为优秀的参者提供了一系列的奖项和机会。例如,获得比高分的学生有可能获得奖章、荣誉证书和奖金等奖励;而表现出色的选手还有机会代表上海市参加国内外的算法竞赛,与来自不同地区的青少进行交流和比拼。 总之,上海市青少算法竞赛YACS)不仅具有促进青少算法能力发展的功能,还为他们提供了一个展示和提升自己的平台。通过参与这个竞赛,青少可以锻炼解决问题的能力、培养创新思维,为未来的学习和职业发展打下坚实的基础。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值