題目描述
開發一個座標計算工具, 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
這個程式看說明感覺挺難的,但是相同了就很簡單,就只是一些簡單的加法操作。
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
while(sc.hasNext()){
String s = sc.nextLine();
String[] a = s.split(";");
int x=0;
int y=0;
for(int i = 0;i<a.length;i++){
if(a[i].charAt(0)=='A'&&a[i].substring(1).matches("[0-9]{1,2}")){
x-= Integer.parseInt(a[i].substring(1));
}
if(a[i].charAt(0)=='S'&&a[i].substring(1).matches("[0-9]{1,2}")){
y-=Integer.parseInt(a[i].substring(1));
}
if(a[i].charAt(0)=='W'&&a[i].substring(1).matches("[0-9]{1,2}")){
y+=Integer.parseInt(a[i].substring(1));
}
if(a[i].charAt(0)=='D'&&a[i].substring(1).matches("[0-9]{1,2}")){
x+=Integer.parseInt(a[i].substring(1));
}
}
System.out.println(x+","+y);
}
}