網易筆試題目-2017-9-25
阿新 • • 發佈:2017-09-25
試題 pre ssi 不同 ring import bit imp ges
1 package demi; 2 3 import java.util.Arrays; 4 import java.util.Scanner; 5 6 public class Main { 7 8 public static void main(String[] args) { 9 10 Scanner in = new Scanner(System.in); 11 int t; 12 t = in.nextInt(); 13 int[] x = new int[t]; 14for (int i = 0; i < t; i ++) { 15 x[i] = in.nextInt(); 16 } 17 System.out.println(t); 18 for (int i = 0; i < t; i ++) { 19 System.out.println(x[i]); 20 } 21 22 for (int i = 0; i < t; i ++) { 23 if (RearrangeDivided(x[i])) {24 System.out.println(x[i] + " Possible"); 25 }else { 26 System.out.println(x[i] + " Impossible"); 27 } 28 } 29 } 30 31 // 判斷一個整數是否有能整除它的混排 32 public static boolean RearrangeDivided(int num) { 33 boolean ret = false; 34 int bit, tmpNum; 35 String str = String.valueOf(num), tmpS; 36 bit = str.length(); 37 if(bit == 0) { 38 return ret; 39 } 40 for (int i = 2; i <= 9; i ++) { 41 tmpNum = num * i; 42 tmpS = String.valueOf(tmpNum); 43 if(isRearrange(str, tmpS)) { 44 ret = true; 45 return ret; 46 } 47 } 48 return ret; 49 } 50 51 // 判斷兩個字符串是否是一個組合的不同排列 52 public static boolean isRearrange(String str1, String str2) { 53 boolean ret = false; 54 55 // 字符串轉成字符數組排序 56 char[] c1 = str1.toCharArray(); 57 char[] c2 = str2.toCharArray(); 58 Arrays.sort(c1); 59 Arrays.sort(c2); 60 61 // 比較內容相等,字符數組轉成字符串比較是否相等 62 String newStr1 = new String(c1); 63 String newStr2 = new String(c2); 64 65 if(newStr1.length() != newStr2.length()){ 66 return ret; 67 }else if (newStr1.equals(newStr2)) { 68 ret = true; 69 } 70 return ret; 71 } 72 73 }
網易筆試題目-2017-9-25