当前位置:网站首页>牛客 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);
}
}
}
边栏推荐
- tensorflow2.0 cnn(layerwise)
- 双边滤波加速「建议收藏」
- 2022年必读的12本机器学习书籍推荐
- What is the difference between BI software in the domestic market?
- ansible学习笔记02
- Implement anti-shake and throttling functions
- what exactly is json (c# json)
- Foreign media right, apple on May be true in inventory
- Character pointer assignment [easy to understand]
- C语言”三子棋“升级版(模式选择+AI下棋)
猜你喜欢
C程序是如何跑起来的01 —— 普通可执行文件的构成
What is the difference between BI software in the domestic market?
Use of radiobutton
Kubernetes常用命令
T - sne + data visualization parts of the network parameters
01 邂逅typescript,环境搭建
t-sne 数据可视化网络中的部分参数+
Premiere Pro 2022 for (pr 2022)v22.5.0
【TypeScript】深入学习TypeScript类型操作
The use of button controls
随机推荐
[Meetup Preview] OpenMLDB+OneFlow: Link feature engineering to model training to accelerate machine learning model development
i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)
Deployment应用生命周期与Pod健康检查
Oracle dynamically registers non-1521 ports
t-sne 数据可视化网络中的部分参数+
6-22 Vulnerability exploit - postgresql database password cracking
Delete table data or clear table
Insert into data table to insert data
MySQL database operations
Doing things software development - the importance of law and understanding of reasonable conclusions
Getting Started with TextBlock Control Basic Tools Usage, Get Started
【Meetup预告】OpenMLDB+OneFlow:链接特征工程到模型训练,加速机器学习模型开发
form 表单提交后,使页面不跳转[通俗易懂]
【7.29】代码源 - 【排列】【石子游戏 II】【Cow and Snacks】【最小生成数】【数列】
使用 Postman 工具高效管理和测试 SAP ABAP OData 服务的试读版
Bilateral filtering acceleration "recommended collection"
国内市场上的BI软件,到底有啥区别
2022年必读的12本机器学习书籍推荐
"Autumn Recruitment Series" MySQL Interview Core 25 Questions (with answers)
leetcode303 Weekly Match Replay