当前位置:网站首页>牛客 HJ17 坐标移动
牛客 HJ17 坐标移动
2022-07-31 15:58:00 【顧棟】
描述
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A; % ; YAD; 等。
下面是一个简单的例子 如:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
处理过程:
起点(0,0)
A10 = (-10,0)
S20 = (-10,-20)
W10 = (-10,-10)
D30 = (20,-10)
x = 无效
A1A = 无效
B10A11 = 无效
一个空 不影响
A10 = (10,-10)
结果 (10, -10)
数据范围:每组输入的字符串长度满足 1 ≤ n ≤ 10000 1\le n \le 10000 1≤n≤10000 ,坐标保证满足 − 2 31 ≤ x , y ≤ 2 31 − 1 {-2^{31} \le x,y \le 2^{31}-1 } −231≤x,y≤231−1,且数字部分仅含正数
输入描述:
一行字符串
输出描述:
最终坐标,以逗号分隔
示例1
输入:
A10;S20;W10;D30;X;A1A;B10A11;;A10;
输出:
10,-10
示例2
输入:
ABC;AKL;DA1;
输出:
0,0
java实现
package nowcoder.x1x;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class HJ017 {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String line = reader.readLine();
if (null != line) {
int x = 0;
int y = 0;
String[] str = line.split(";");
for (String s : str) {
if ("".equals(s) || s.length() > 3) {
continue;
}
if (!s.startsWith("A")
&& !s.startsWith("D")
&& !s.startsWith("W")
&& !s.startsWith("S")) {
continue;
}
int v = 0;
for (int i = 1; i < s.length(); i++) {
// 通过ASCII码将字符与数值进行转换
int t = s.charAt(i) - '0';
if (t >= 0 && t <= 9) {
//获取合理值
if (i == 1 && s.length() != 2) {
v += t * 10;
} else {
v += t;
}
} else {
v = 0;
break;
}
}
char c = s.charAt(0);
switch (c) {
case 'A':
x = x - v;
break;
case 'D':
x = x + v;
break;
case 'W':
y = y + v;
break;
case 'S':
y = y - v;
break;
default:
break;
}
}
System.out.println(x + "," + y);
}
}
}
边栏推荐
- Implementing click on the 3D model in RenderTexture in Unity
- form 表单提交后,使页面不跳转[通俗易懂]
- 数据表插入数据insert into
- The arm button controls the flashing of the led light (embedded button experiment report)
- 苹果官网样式调整 结账时产品图片“巨大化”
- Linux查看redis版本(查看mongodb版本)
- 2020 WeChat applet decompilation tutorial (can applet decompile source code be used)
- 网站漏洞修复服务商关于越权漏洞分析
- TextBlock控件入门基础工具使用用法,取上法入门
- T - sne + data visualization parts of the network parameters
猜你喜欢

Getting Started with TextBlock Control Basic Tools Usage, Get Started

基于C语言的编译器设计与实现

C language - function

C程序是如何跑起来的01 —— 普通可执行文件的构成

Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency

tooltips使用教程(鼠标悬停时显示提示)

Tencent Cloud Deployment----DevOps

The 2nd China PWA Developer Day
![[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development](/img/f6/311d5a4c70993df6291250d2025d3f.jpg)
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development

【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
随机推荐
Applicable Scenarios of Multi-Master Replication (1) - Multi-IDC
LevelSequence源码分析
MySQL常用语句整理
国内市场上的BI软件,到底有啥区别
单细胞测序流程(单细胞rna测序)
Deployment应用生命周期与Pod健康检查
更新数据表update
Browser's built-in color picker
【7.29】Code Source - 【Arrangement】【Stone Game II】【Cow and Snacks】【Minimum Number of Spawns】【Sequence】
【TypeScript】深入学习TypeScript类型操作
JVM parameter analysis Xmx, Xms, Xmn, NewRatio, SurvivorRatio, PermSize, PrintGC "recommended collection"
How Redis handles concurrent access
Dialogue with Zhuang Biaowei: The first lesson of open source
Graham's Scan method for solving convex hull problems
多主复制下处理写冲突(4)-多主复制拓扑
基于C语言的编译器设计与实现
.NET 20周年专访 - 张善友:.NET 技术是如何赋能并改变世界的
7. Summary of common interview questions
type of timer
Baidu cloud web speed playback (is there any website available)