Niu Ke net HUAWEI machine test exercise 17 coordinate movement

Keywords: Java less

Niu Ke net HUAWEI machine test exercise 17 coordinate movement

Title Description

Develop a coordinate calculating tool. A means to move left, D means to move right, W means to move up, S means to move down. Move from point (0,0), read some coordinates from the input string, and output the final input results to the output file.

Input:

The legal coordinates are between A (or D or W or S) +numeric (less than two) coordinates; separated.

Illegal coordinate points need to be discarded. For example, AA10; A1A;%; YAD; etc.

Here is a simple example.

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

Processing process:

Starting point (0,0)

   A10   =  (-10,0)

   S20   =  (-10,-20)

   W10  =  (-10,-10)

   D30  =  (20,-10)

   x = invalid

   A1A = invalid

  B10A11 = invalid

  A vacancy does not affect

   A10  =  (10,-10)

Results (10, -10)

Input Description:

A line of strings

Output Description:

Final coordinates, separated by

Example 1

input

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

output

10,-10

Train of thought:

Seeing such a question, it is easy to think of how to judge the abnormal situation. But in fact, there are more ingenious ways to use exceptions.

If it works properly, proceed; otherwise, just as with capture, proceed with processing using continue.

Normally, it's easier to get the first character ch and the value num.

If ch=='A', abscissa minus num

If ch=='D', add num to abscissa

If ch=='W', add num to the ordinates.

If ch=='S', the ordinate minus num

#### Solution code:

import java.util.Scanner;
import java.lang.Integer;

public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNextLine())
        {
            String str = sc.nextLine();
            String[] arr = str.split(";");
            int x=0;
            int y=0;
            for(int i=0;i<arr.length;i++)
            {
                //Judging whether it is a legal character
                try{
                    int b = Integer.parseInt(arr[i].substring(1));
                    char dir = arr[i].charAt(0);
                    if(dir=='A'){
                        x-=b;
                    }
                    if(dir=='W'){
                        y+=b;
                    }
                    if(dir=='S'){
                        y-=b;
                    }
                    if(dir=='D'){
                        x+=b;
                    }
                }catch(Exception e){
                    continue;
                }
            }
            System.out.println(x+","+y);
    }
    }
}
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);
             
        }
    }
}

Posted by smilesmita on Sat, 05 Oct 2019 16:19:58 -0700