当前位置:网站首页>牛客 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);
}
}
}
边栏推荐
- Summary of the implementation method of string inversion "recommended collection"
- Implementing distributed locks based on Redis (SETNX), case: Solving oversold orders under high concurrency
- mysql黑窗口~建库建表
- Codeforces Round #796 (Div. 2)(A-D)
- After the form is submitted, the page does not jump [easy to understand]
- 小程序:matlab解微分方程「建议收藏」
- After Grafana is installed, the web opens and reports an error
- button控件的使用
- Internet banking stolen?This article tells you how to use online banking safely
- 更新数据表update
猜你喜欢
The 2nd China PWA Developer Day
Tencent Cloud Deployment----DevOps
国内市场上的BI软件,到底有啥区别
全新宝马3系上市,安全、舒适一个不落
C language "the third is" upgrade (mode selection + AI chess)
Premiere Pro 2022 for (pr 2022)v22.5.0
Internet banking stolen?This article tells you how to use online banking safely
Kubernetes常用命令
Qt practical cases (54) - using transparency QPixmap design pictures
【TypeScript】深入学习TypeScript类型操作
随机推荐
更新数据表update
入职一个月反思
基于Redis(SETNX)实现分布式锁,案例:解决高并发下的订单超卖,秒杀
Graham's Scan method for solving convex hull problems
C语言-函数
长得很怪的箱图
小程序:matlab解微分方程「建议收藏」
Replication Latency Case (3) - Monotonic Read
Foreign media right, apple on May be true in inventory
腾讯云部署----DevOps
MySQL数据库操作
How to switch remote server in gerrit
go图书管理系统
Why is the field of hacking almost filled with boys?
6-22漏洞利用-postgresql数据库密码破解
Codeforces Round #796 (Div. 2)(A-D)
Premiere Pro 2022 for (pr 2022)v22.5.0
百度网盘网页版加速播放(有可用的网站吗)
Kubernetes常用命令
Linux check redis version (check mongodb version)