public class JAVA { // 輸入緩衝區 (緩衝空間需要依照題目調整) public static byte[] cinbuf = new byte[1024]; // 讀取一個單字 (英文姓名包含空白時請不要用) public static String readToken() { int offset = 0; int bytedata = -1; try { // 略過非單字的字元 '\t','\n','\r',' ' bytedata = System.in.read(); while(bytedata==9||bytedata==10||bytedata==13||bytedata==32) { bytedata = System.in.read(); } // 載入單字的字元 while(bytedata!=-1) { if(bytedata==9||bytedata==10||bytedata==13||bytedata==32) { break; } else { cinbuf[offset++] = (byte)bytedata; } bytedata = System.in.read(); } } catch(Exception e) {} if(offset+bytedata==-1) return null; // 串流結束 return new String(cinbuf,0,offset); } // 讀取一行 public static String readLine() { int offset = 0; int bytedata = -1; try { // 載入整行 bytedata = System.in.read(); while(bytedata!=-1) { if(bytedata==10) { break; } else { cinbuf[offset++] = (byte)bytedata; } bytedata = System.in.read(); } } catch(Exception e) {} if(offset+bytedata==-1) return null; // 串流結束 if(cinbuf[offset-1]=='\r') offset--; // window 要去除 '\r' 字元 return new String(cinbuf,0,offset); } public static void main(String[] args) { int testCase = parseInt(readLine()); for(int i = 0; i < testCase; i++) { int a = parseInt(readToken()); int b = parseInt(readToken()); System.out.println(countStep(b-a)); } } public static int countStep(int diff) { int step = 0; if(diff == 0) return 0; step = (int)(Math.sqrt(diff)); if(step * step == diff) step = step * 2 - 1; else if(step * step + step < diff) step = step * 2 + 1; else step = step * 2; return step; } }
2010/02/01
Steps
計算兩個數字的距離的最短步數,每一步行走距離只能相差1或是相等。
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment