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;
}

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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值