NYOJ题目889求距离
------------------------------------------
题目可以抽象一下为计算坐标系上两点间的距离,即
AC代码:
1 import java.awt.Point; 2 import java.io.BufferedReader; 3 import java.io.IOException; 4 import java.io.InputStreamReader; 5 6 public class Main { 7 8 public static void main(String[] args) throws IOException { 9 10 BufferedReader reader=new BufferedReader(new InputStreamReader(System.in)); 11 12 boolean first=true; 13 while(first || reader.ready()){ 14 first=false; 15 String s1=reader.readLine(); 16 String s2=reader.readLine(); 17 double ans=solve(s1,s2); 18 System.out.printf("%.2f\n",ans); 19 } 20 } 21 22 public static double solve(String s1,String s2){ 23 Point p1=compile(s1); 24 Point p2=compile(s2); 25 return Math.sqrt(Math.pow(p1.x-p2.x,2)+Math.pow(p1.y-p2.y,2)); 26 } 27 28 public static Point compile(String s){ 29 char cs[]=s.replaceAll(" ","").toCharArray(); 30 Point p=new Point(); 31 for(int i=0;i<cs.length;i++){ 32 switch(cs[i]){ 33 case 'W': 34 p.x--; 35 break; 36 case 'E': 37 p.x++; 38 break; 39 case 'S': 40 p.y--; 41 break; 42 case 'N': 43 p.y++; 44 break; 45 } 46 } 47 return p; 48 } 49 50 }