• 牛客网——华为机试(题17:坐标移动)(Java)


    题目描述:

    开发一个坐标计算工具, 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:

    输入:

    A10;S20;W10;D30;X;A1A;B10A11;;A10;

    输出:

    10,-10

    代码: 

    import java.util.Scanner;
    
    public class Main {
    	public static void main ( String[] args ) {
    		Scanner in = new Scanner( System.in );
    		while ( in.hasNextLine() ) {
    		String s1 = in.nextLine();
    		String s[] = s1.split(";");
    		int x = 0;
    		int y = 0;
    		for( int i = 0 ; i < s.length ; i++ ) {
    			if ( (s[i].length() == 3 && ( (s[i].charAt(0) == 'A' || s[i].charAt(0) == 'S' || s[i].charAt(0) == 'W' || s[i].charAt(0) == 'D') && 
    						( s[i].charAt(1) >= '0' && s[i].charAt(1) <= '9') && ( s[i].charAt(2) >= '0' && s[i].charAt(2) <= '9' ) )) || 
    					( s[i].length() == 2 && ( (s[i].charAt(0) == 'A' || s[i].charAt(0) == 'S' || s[i].charAt(0) == 'W' || s[i].charAt(0) == 'D') && 
    							( s[i].charAt(1) >= '0' && s[i].charAt(1) <= '9') ) ) ) {
    				if ( s[i].charAt(0) == 'A' ) {
    					x -= Integer.valueOf( s[i].substring(1) );
    				}
    				if ( s[i].charAt(0) == 'D' ) {
    					x += Integer.valueOf( s[i].substring(1) );
    				}
    				if ( s[i].charAt(0) == 'S' ) {
    					y -= Integer.valueOf( s[i].substring(1) );
    				}
    				if ( s[i].charAt(0) == 'W' ) {
    					y += Integer.valueOf( s[i].substring(1) );
    				}
    			}
    		}
    		System.out.println( x +","+ y );
    		}
    	}
    }
    
  • 相关阅读:
    区间DP+next求循环节 uva 6876
    连分数(分数类模板) uva6875
    二分+最短路 uvalive 3270 Simplified GSM Network(推荐)
    叉积判断 POJ1696
    树形DP+树状数组 HDU 5877 Weak Pair
    团 大连网赛 1007 Friends and Enemies
    微信支付少一分钱
    数据类型的转换
    什么是机器学习
    config.m4
  • 原文地址:https://www.cnblogs.com/cg-bestwishes/p/10681161.html
Copyright © 2020-2023  润新知