目錄
前言:以下代碼僅供參考,若有錯誤歡迎指正哦~
1 階乘位數
階乘位數 9的階乘等於:362880 它的二進制表示為:1011000100110000000 這個數字共有19位。 請你計算,9999 的階乘的二進制表示一共有多少位? 注意:需要提交的是一個整數,不要填寫任何無關內容(比如說明解釋等) 答案:118445
1 import java.math.BigInteger; 2 3 public class Main { 4 5 public static void main(String[] args) { 6 BigInteger result = new BigInteger("1"); 7 for(int i = 1;i <= 9999;i++) { 8 BigInteger temp = new BigInteger(""+i); 9 result = result.multiply(temp); 10 } 11 long count = 0; 12 BigInteger a = new BigInteger("2"); 13 while(result.compareTo(BigInteger.ZERO) > 0) { 14 count++; 15 result = result.divide(a); 16 //System.out.println("count = "+count); 17 } 18 System.out.println(count); 19 } 20 }
2 湊平方數
湊平方數 把0~9這10個數字,分成多個組,每個組恰好是一個平方數,這是能夠辦到的。 比如:0, 36, 5948721 再比如: 1098524736 1, 25, 6390784 0, 4, 289, 15376 等等... 注意,0可以作為獨立的數字,但不能作為多位數字的開始。 分組時,必須用完所有的數字,不能重復,不能遺漏。 如果不計較小組內數據的先后順序,請問有多少種不同的分組方案? 注意:需要提交的是一個整數,不要填寫多余內容。 答案:300
1 import java.util.Arrays; 2 import java.util.HashSet; 3 4 public class Main { 5 public static HashSet<String> set = new HashSet<String>(); 6 7 public void swap(int[] A, int i, int j) { 8 int temp = A[i]; 9 A[i] = A[j]; 10 A[j] = temp; 11 } 12 13 public void dfsAllSort(int[] A, int step) { 14 if(step == A.length) { 15 long[] B = new long[A.length]; 16 dfsSqrtNum(A, 0, B, 0); 17 return; 18 } else { 19 for(int i = step;i < A.length;i++) { 20 swap(A, i, step); 21 dfsAllSort(A, step + 1); 22 swap(A, i, step); 23 } 24 } 25 } 26 27 public void dfsSqrtNum(int[] A, int step, long[] B, int num) { 28 if(step == A.length) { 29 StringBuffer s = new StringBuffer(""); 30 long[] arrayA = new long[num]; 31 for(int i = 0;i < num;i++) 32 arrayA[i] = B[i]; 33 Arrays.sort(arrayA); 34 for(int i = 0;i < num;i++) { 35 s.append(arrayA[i]); 36 if(i != num - 1) 37 s.append("-"); 38 } 39 set.add(s.toString()); 40 return; 41 } 42 43 if(A[step] == 0) { 44 B[num] = 0; 45 dfsSqrtNum(A, step + 1, B, num + 1); 46 } else { 47 long sum = 0; 48 for(int i = step;i < A.length;i++) { 49 sum = sum * 10 + A[i]; 50 double son = Math.sqrt(sum * 1.0); 51 long a = (long) son; 52 if(a == son) { 53 B[num] = sum; 54 dfsSqrtNum(A, i + 1, B, num + 1); 55 } 56 } 57 } 58 59 } 60 61 public static void main(String[] args) { 62 Main test = new Main(); 63 int[] A = {0,1,2,3,4,5,6,7,8,9}; 64 test.dfsAllSort(A, 0); 65 System.out.println(set.size()); 66 } 67 }
3 棋子換位
棋子換位 有n個棋子A,n個棋子B,在棋盤上排成一行。 它們中間隔着一個空位,用“.”表示,比如: AAA.BBB 現在需要所有的A棋子和B棋子交換位置。 移動棋子的規則是: 1. A棋子只能往右邊移動,B棋子只能往左邊移動。 2. 每個棋子可以移動到相鄰的空位。 3. 每個棋子可以跳過相異的一個棋子落入空位(A跳過B或者B跳過A)。 AAA.BBB 可以走法: 移動A ==> AA.ABBB 移動B ==> AAAB.BB 跳走的例子: AA.ABBB ==> AABA.BB 以下的程序完成了AB換位的功能,請仔細閱讀分析源碼,填寫划線部分缺失的內容。 public class Main { static void move(char[] data, int from, int to) { data[to] = data[from]; data[from] = '.'; } static boolean valid(char[] data, int k) { if(k<0 || k>=data.length) return false; return true; } static void f(char[] data) { while(true){ boolean tag = false; for(int i=0; i<data.length; i++){ int dd = 0; // 移動方向 if(data[i]=='.') continue; if(data[i]=='A') dd = 1; if(data[i]=='B') dd = -1; if(valid(data, i+dd) && valid(data,i+dd+dd) && data[i+dd]!=data[i] && data[i+dd+dd]=='.'){ // 如果能跳... move(data, i, i+dd+dd); System.out.println(new String(data)); tag = true; break; } } if(tag) continue; for(int i=0; i<data.length; i++){ int dd = 0; // `移動方向 if(data[i]=='.') continue; if(data[i]=='A') dd = 1; if(data[i]=='B') dd = -1; if(valid(data, i+dd) && data[i+dd]=='.'){ // 如果能移動... if( _____________________ ) continue; //填空位置 move(data, i, i+dd); System.out.println(new String(data)); tag = true; break; } } if(tag==false) break; } } public static void main(String[] args) { char[] data = "AAA.BBB".toCharArray(); f(data); } } 注意:只提交划線部分缺少的代碼,不要復制已有代碼或填寫任何多余內容。 答案:valid(data,i+dd+dd) && valid(data, i-dd) && data[i-dd] == data[i+dd+dd]
4 機器人塔
機器人塔 X星球的機器人表演拉拉隊有兩種服裝,A和B。 他們這次表演的是搭機器人塔。 類似: A B B A B A A A B B B B B A B A B A B B A 隊內的組塔規則是: A 只能站在 AA 或 BB 的肩上。 B 只能站在 AB 或 BA 的肩上。 你的任務是幫助拉拉隊計算一下,在給定A與B的人數時,可以組成多少種花樣的塔。 輸入一行兩個整數 M 和 N,空格分開(0<M,N<500),分別表示A、B的人數,保證人數合理性。 要求輸出一個整數,表示可以產生的花樣種數。 例如: 用戶輸入: 1 2 程序應該輸出: 3 再例如: 用戶輸入: 3 3 程序應該輸出: 4 資源約定: 峰值內存消耗 < 256M CPU消耗 < 1000ms 請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。 所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。 注意:不要使用package語句。不要使用jdk1.7及以上版本的特性。 注意:主類的名字必須是:Main,否則按無效代碼處理。
1 import java.util.Scanner; 2 3 public class Main { 4 public static int m, n; 5 public static int[][] value; //此處用數字1代表A,數字2代表B 6 public static int len, count = 0; 7 8 public void check() { 9 for(int i = len - 2, t = 1;i >= 0;i--, t++) 10 for(int j = 0;j < len - t;j++) { 11 if(value[i + 1][j] == value[i + 1][j + 1]) 12 value[i][j] = 1; 13 else 14 value[i][j] = 2; 15 } 16 int sumA = 0, sumB = 0; 17 for(int i = 0, t = len - 1;i < len;i++, t--) 18 for(int j = 0;j < len - t;j++) { 19 if(value[i][j] == 1) 20 sumA++; 21 else if(value[i][j] == 2) 22 sumB++; 23 } 24 if(sumA == m && sumB == n) 25 count++; 26 } 27 28 public void dfs(int step) { 29 if(step == len) { 30 check(); 31 return; 32 } else { 33 for(int i = 1;i <= 2;i++) { 34 value[len - 1][step] = i; //確定三角形最底層一行,即可確定整個三角形 35 dfs(step + 1); 36 } 37 } 38 } 39 40 41 public static void main(String[] args) { 42 Main test = new Main(); 43 Scanner in = new Scanner(System.in); 44 m = in.nextInt(); 45 n = in.nextInt(); 46 for(int i = 1;i < 100;i++) { //滿足三角形規則,可知i層,則(i + 1) * i / 2 = m + n 47 int a = (i + 1) * i / 2; 48 if(a == m + n) { 49 len = i; 50 value = new int[a][a]; 51 break; 52 } 53 } 54 test.dfs(0); 55 System.out.println(count); 56 } 57 }