小喵們很喜歡把本身裝進容器裏的(例如碗),可是要是碗的周長比喵的身長還短,它們就進不去了。如今告訴你它們的身長,和碗的半徑,請判斷一下可否到碗裏去。java
輸入有多組數據。每組數據包含兩個整數n (1≤n≤2^128) 和r (1≤r≤2^128),分別表明喵的身長和碗的半徑。圓周率使用3.14。算法
對應每一組數據,若是喵能裝進碗裏就輸出「Yes」;不然輸出「No」。數組
6 1 7 1 9876543210 1234567890
Yes No No
題目中輸入的數值比較大,因此不能使用通常的字數字進行計算,要使用大整數乘法思想。
假設貓的長度是m(m=xi−1xi−2…x0),碗的半徑是n(n=xj−1xj−2…x0),π取3.14。只要比較n和2*m*π的大小就能夠判斷貓是否能夠進入碗裏。由於m、n不能使用數字來表示,能夠使用數組a、b來表示他們。同時由於π是小數,要將m、n、π統一成整數進行運算。能夠將m放大100倍,π放大100倍。a[0]=0,a[1]=0,表示放大100倍,a[k]表示m中的xk−2,b[k]表示n中的xk。π使用數組PI表示。PI[0]=4,PI[1]=1,PI[2]=3。計算2*b*PI(結果爲r)再比較r與n的大小便可。測試
import java.util.Scanner; /** * Declaration: All Rights Reserved !!! */ public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Scanner scanner = new Scanner(Main.class.getClassLoader().getResourceAsStream("data.txt")); while (scanner.hasNext()) { String cat = scanner.next(); String bowl = scanner.next(); System.out.println(toTheBowl(cat, bowl)); } scanner.close(); } /** * 判斷貓是否能夠進到碗裏 * * @param cat 貓的長度 * @param bowl 碗的半徑 * @return Yes:貓能夠到碗裏,false:貓不能夠到碗裏 */ private static String toTheBowl(String cat, String bowl) { // 200*PI int[] PI = {8, 2, 6}; // cat的值要放大100倍 int[] n = new int[cat.length() + 2]; int[] m = new int[bowl.length()]; // 將cat轉換成數值,而且放大100倍 for (int i = 0; i < cat.length(); i++) { n[i + 2] = cat.charAt(cat.length() - i - 1) - '0'; } // bowl轉換成數值 for (int i = 0; i < bowl.length(); i++) { m[i] = bowl.charAt(bowl.length() - i - 1) - '0'; } int[] r = calculate(m, PI); if (compare(r, n) >= 0) { return "Yes"; } else { return "No"; } } /** * 比較兩個整數是否相等,下標由小到大表示由低位到高位,忽略最高有效位上的前導0 * * @param m 整數 * @param n 整數 * @return m > n返回1,m = n返回0,m < n返回-1 */ private static int compare(int[] m, int[] n) { if (m == null && n == null) { return 0; } // null最小 if (m == null) { return -1; } if (n == null) { return 1; } int lastM = m.length - 1; int lastN = n.length - 1; // 找m的最高有效位的位置,至少有一位 while (lastM >= 1 && m[lastM] == 0) { lastM--; } // 找n的最高有效位的位置,至少有一位 while (lastN >= 1 && n[lastN] == 0) { lastN--; } // m的數位比n多,說明m比n大 if (lastM > lastN) { return 1; } // m的數位比n少,說明m比n小 else if (lastM < lastN) { return -1; } else { // 位數同樣,比較每個數位上的值,從高位到低位進行比較 for (int i = lastM; i >= 0; i--) { if (m[i] > n[i]) { return 1; } else if (m[i] < n[i]) { return -1; } } return 0; } } /** * 兩個數相乘 * * @param m 乘數 * @param n 乘數 * @return 結果 */ private static int[] calculate(int[] m, int[] n) { if (n == null || n.length < 1 || m == null || m.length < 1) { return null; } // 結果最多的位數 int[] r = new int[m.length + n.length]; // 來自低位的進位 int c; int t; int k; for (int i = 0; i < n.length; i++) { // 計算n[i]*m if (n[i] == 0) { continue; } c = 0; for (int j = 0; j < m.length; j++) { t = n[i] * m[j] + r[i + j] + c; r[i + j] = t % 10; c = t / 10; } // 若是還有進位要繼續處理 k = i + m.length; while (c != 0) { t = c + r[k]; r[k] = t % 10; c = t / 10; k++; } } return r; } }