华为在线编程系列-坐标移动
来源:互联网 发布:芮时原创女装 知乎 编辑:程序博客网 时间:2024/04/30 18:31
题目描述:
开发一个坐标计算工具, A表示向左移动,D表示向右移动,W表示向上移动,S表示向下移动。从(0,0)点开始移动,从输入字符串里面读取一些坐标,并将最终输入结果输出到输出文件里面。
输入:
合法坐标为A(或者D或者W或者S) + 数字(两位以内)
坐标之间以;分隔。
非法坐标点需要进行丢弃。如AA10; A1A;
下面是一个简单的例子 如:
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)
我的思路:首先写一个方法来判断输入的该字符串是否有效,如果有效在分四种情况对横坐标和纵坐标进行移动
牛客上没有AC但是IDE输出结果正确(牛客不输出东西)
import java.util.Scanner;/** * Created by hmh on 2017/4/1. * 坐标移动(在牛客平台不能通过) */public class MovePosition { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); String str = scanner.nextLine(); String[] arr = str.split(";"); int[][] result = new int[1][2]; for(int i = 0; i < arr.length; i++){ if(isValid(arr[i])){ switch (arr[i].charAt(0)){ case 'A': result[0][0] -= Integer.parseInt(arr[i].substring(1)); break; case 'D': result[0][0] += Integer.parseInt(arr[i].substring(1)); break; case 'W': result[0][1] += Integer.parseInt(arr[i].substring(1)); break; case 'S': result[0][1] -= Integer.parseInt(arr[i].substring(1)); break; } }else{ continue; } } System.out.print(result[0][0] + "," +result[0][1]); } public static boolean isValid(String string){ if(string.equals("") || string == null){ return false; } if(string.length() > 3){ return false; } if (string.toUpperCase().charAt(0) <= 'Z' && string.toUpperCase().charAt(0) >= 'A') { for(int i = 1; i < string.length(); i++){ if(string.charAt(i) > '9' || string.charAt(i) < '0'){ return false; } } } return true; }}
更新!!!!!!
这次终于AC了,很多情况下,在牛客上跑的程序没有输出,是因为测试用例的输入问题(下次遇到IDE里运行正确,而牛客网没有内容输出,就可以试一下System.out.println()或者加上scanner.hasNextLine的判断)
import java.util.Scanner;/** * Created by hmh on 2017/4/1. * */public class MovePosition { public static void main(String[] args){ Scanner scanner = new Scanner(System.in); //String str = ""; while(scanner.hasNextLine()){ String str = scanner.nextLine(); String[] arr = str.split(";"); // int[][] result = new int[1][2]; int x = 0; int y = 0; for(int i = 0; i < arr.length; i++){ if(isValid(arr[i])){ switch (arr[i].charAt(0)){ case 'A': x -= Integer.parseInt(arr[i].substring(1)); break; case 'D': x += Integer.parseInt(arr[i].substring(1)); break; case 'W': y += Integer.parseInt(arr[i].substring(1)); break; case 'S': y -= Integer.parseInt(arr[i].substring(1)); break; } }else{ continue; } } System.out.println(x + "," + y); } } public static boolean isValid(String string){ if(string.equals("") || string == null){ return false; } if(string.length() > 3){ return false; } if (string.toUpperCase().charAt(0) <= 'Z' && string.toUpperCase().charAt(0) >= 'A') { for(int i = 1; i < string.length(); i++){ if(string.charAt(i) > '9' || string.charAt(i) < '0'){ return false; } } } return true; }}
0 0
- 华为在线编程系列-坐标移动
- 华为在线编程系列-最小公倍数
- 华为机试在线训练-牛客网(10)坐标移动
- 华为在线编程系列-字符串分割
- 华为在线编程系列-密码合格验证
- 华为在线编程系列-进制转换
- 华为在线编程系列-简单密码
- 华为在线编程系列-数据分类处理
- 华为在线编程系列-字符串排序
- 华为在线编程系列-质数因子
- 华为在线编程系列-明明的随机数
- 华为在线编程系列-字符串加密解密
- 华为测试 坐标移动
- 华为oj 坐标移动
- 华为OJ 坐标移动
- 华为oj 坐标移动
- 华为Oj 坐标移动
- 华为oj 坐标移动
- cartographer算法简析
- C#单元测试常见问题及解决方案
- 安卓笔记侠周报|Google 上线开源网站,你还爱 GitHub 吗
- 省选算法汇集
- BestCoder Round #93
- 华为在线编程系列-坐标移动
- 用除法解决问题练习
- 用除法解决问题练习
- SVN trunk(主线) branch(分支) tag(标记) 用法详解和详细操作步骤
- 50道hdu基础的搜索题目
- 虚拟机Ubuntu开启出现:An internal error occurred during:
- 大整数的加减乘除运算
- android之对象存储在本地或者sdcard
- Android系统服务:AppUsageStatistics