Huawei Online Programming Question Series-17- Coordinate Movement

Problem Description:
Problem Description
Problem Description

1. The question involves knowledge points.

  • String handling.

2. Solve it yourself.

  • Get the string according to the line, string.split(";")split the use of the string, and analyze the rationality of each unit.
  • If it is reasonable and according to the requirements of the topic, execute it.
package com.chaoxiong.niuke.huawei;
import java.util.Objects;
import java.util.Scanner;
public class HuaWei_17 {
    private static final String Split = ";";
    public static void main(String[] args) {
        String string = null;
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            string = in.nextLine();
            int []pointArr = {0,0};
            String[] strArray = string.split(Split);
            for (String each:strArray){
                int []flag = isLegel(each);//[(1表示合法 -1表示不合法),(其他表示移动的多少 -1表示不合法)]
                if(flag[0]==1&&flag[1]!=-1){
                    //输入合法
                    char []charArr = each.toCharArray();
                    char dir = charArr[0];
                    int step = flag[1];
                    nextPoint(pointArr,dir,step);
                }
            }
            System.out.println(pointArr[0]+","+pointArr[1]);
        }
    }

    private static void nextPoint(int[] pointArr, char dir, int step) {
        switch (dir){
            case 'A'://向左
                pointArr[0] = pointArr[0]-step;
                break;
            case 'D'://向左
                pointArr[0] = pointArr[0]+step;
                break;
            case 'W'://向左
                pointArr[1] = pointArr[1]+step;
                break;
            case 'S'://向左
                pointArr[1] = pointArr[1]-step;
                break;
        }
//        System.out.println(pointArr[0]+" "+pointArr[1]);
    }

    private static int[] isLegel(String each) {
//        AA10;  A1A;  $%$;  YAD; 则为不合法.
        //1. 第一位必须是 A,D,W,S中的一个
        //2. 之后的必须是数字.
        int []falg = {-1,-1};
        if(Objects.equals(each.trim(), "") || each.length() < 2){
            //不合法
            falg[0]=-1;
        }else {
            char[] charArr = each.toCharArray();
            //判断第一位
            if(inCharSet(charArr[0])){
                falg[0]=0;
                falg[1]=canConvetInt(new String(charArr, 1, charArr.length - 1));
            }else {
                //不合法
                falg[0]=-1;
            }
        }
        return falg;
    }

    private static int canConvetInt(String s) {
        // 可以装换成整数返回true 不行返回false.
        int result = -1;
        try {
            result = Integer.parseInt(s);
            return result;
        }catch (Exception e){
            return result;
        }
    }

    private static boolean inCharSet(char c) {
        return c == 'A' || c == 'D' || c == 'W' || c == 'S';
    }
}

3. Quality answers.

import java.util.*;
public class Main{
    public static void main(String[]args){
        Scanner scan=new Scanner(System.in);
        while(scan.hasNextLine()){
            int x=0;
            int y=0;
            String[]tokens=scan.nextLine().split(";");
            for(int i=0;i<tokens.length;i++){
                try{
                    int b=Integer.parseInt(tokens[i].substring(1,tokens[i].length()));
                    if(tokens[i].charAt(0)=='A'){
                        x-=b;
                    }
                     if(tokens[i].charAt(0)=='W'){
                        y+=b;
                    }
                     if(tokens[i].charAt(0)=='S'){
                        y-=b;
                    }
                     if(tokens[i].charAt(0)=='D'){
                        x+=b;
                    }
                }catch(Exception e){
                    continue;
                }
            }
            System.out.println(x+","+y);

        }
    }
}

4. Summary of this question.

In contrast, the solution method in the excellent solution is much simpler.
Because there are only four reverses, all of them can only be judged by the four situations.
Directly all houses and exceptions, and those that do not meet the conditions may cause exceptions, just ignore them .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325966678&siteId=291194637