zoukankan      html  css  js  c++  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)

    知识点 字符串
    运行时间限制 0M
    内存限制 0
    输入

    一行字符串

    输出

    最终坐标,以,分隔

    样例输入 A10;S20;W10;D30;X;A1A;B10A11;;A10;
    样例输出 10,-10
    package com.oj;
    
    import java.util.Scanner;
    
    
    public class TestOj {
    	public static void main(String[] args) {
    		Scanner in = new Scanner(System.in);
    		String input = in.nextLine();
    		String[] steps = input.split(";");
    		//System.out.println(steps.length);
    		int posx = 0,posy = 0;
    		for(String temp:steps){
    			if(temp.length()<1)
    				continue;
    			char direction = temp.charAt(0);
    			String value = temp.substring(1,temp.length());
    			int num;
    			try{
    				num = Integer.parseInt(value);
    			}catch(Exception e){
    				continue;
    			}
    			switch(direction){
    				case 'A':
    					posx -= num;
    					break;
    				case 'D':
    					posx +=	num;
    					break;
    				case 'W':
    					posy += num;
    					break;
    				case 'S':
    					posy -= num;
    					break;
    			}
    		}
    			System.out.println(posx+","+posy);
    	}
    }
    

      

  • 相关阅读:
    ui5 call view or method from another view
    vuejs helloworld
    vuejs v-model
    vuejs v-bind
    vuejs on
    vuejs fatherandson
    vuejs scope
    vuejs keep-alive
    VLAN虚拟局域网
    网线的制作
  • 原文地址:https://www.cnblogs.com/lxk2010012997/p/5347721.html
Copyright © 2011-2022 走看看