目錄
前言:以下試題解答代碼部分僅供參考,若有不當之處,還請路過的同學提醒一下~
1 武功秘籍
標題:武功秘籍 小明到X山洞探險,撿到一本有破損的武功秘籍(2000多頁!當然是偽造的)。他注意到:書的第10頁和第11頁在同一張紙上,但第11頁和第12頁不在同一張紙上。 小明只想練習該書的第81頁到第92頁的武功,又不想帶着整本書。請問他至少要撕下多少張紙帶走? 這是個整數,請通過瀏覽器提交該數字,不要填寫任何多余的內容。 7
2 切面條
標題:切面條 一根高筋拉面,中間切一刀,可以得到2根面條。 如果先對折1次,中間切一刀,可以得到3根面條。 如果連續對折2次,中間切一刀,可以得到5根面條。 那么,連續對折10次,中間切一刀,會得到多少面條呢? 答案是個整數,請通過瀏覽器提交答案。不要填寫任何多余的內容。 2^10 + 1 = 1025
3 猜字母
標題:猜字母
把abcd...s共19個字母組成的序列重復拼接106次,得到長度為2014的串。
接下來刪除第1個字母(即開頭的字母a),以及第3個,第5個等所有奇數位置的字母。
得到的新串再進行刪除奇數位置字母的動作。如此下去,最后只剩下一個字母,請寫出該字母。
答案是一個小寫字母,請通過瀏覽器提交答案。不要填寫任何多余的內容。
q
import java.util.ArrayList; public class Main { public static void main(String[] args) { ArrayList<Character> list = new ArrayList<Character>(); list.add('0'); //第0位元素 for(int i = 1;i <= 106;i++) { for(int j = 0;j < 19;j++) { char temp = 'a'; temp = (char) (temp + j); list.add(temp); } } for(int i = 1;i <=19;i++) System.out.print(list.get(i)+" "); System.out.println(); while(list.size() > 2) { ArrayList<Character> tempList = new ArrayList<Character>(); tempList.add('0'); for(int i = 2;i < list.size();i = i + 2) { tempList.add(list.get(i)); } list = tempList; } System.out.println(list.size()+", "+list.get(0)+", "+list.get(1)); } }
4 大衍數列
標題:大衍數列 中國古代文獻中,曾記載過“大衍數列”, 主要用於解釋中國傳統文化中的太極衍生原理。 它的前幾項是:0、2、4、8、12、18、24、32、40、50 ... 其規律是:對偶數項,是序號平方再除2,奇數項,是序號平方減1再除2。 以下的代碼打印出了大衍數列的前 100 項。 for(int i=1; i<100; i++) { if(________________) //填空 System.out.println(i*i/2); else System.out.println((i*i-1)/2); } 請填寫划線部分缺失的代碼。通過瀏覽器提交答案。 注意:不要填寫題面已有的內容,也不要填寫任何說明、解釋文字。 i % 2 == 0
5 圓周率
標題:圓周率 數學發展歷史上,圓周率的計算曾有許多有趣甚至是傳奇的故事。其中許多方法都涉及無窮級數。 圖1.png中所示,就是一種用連分數的形式表示的圓周率求法。 下面的程序實現了該求解方法。實際上數列的收斂對x的初始值 並不敏感。 結果打印出圓周率近似值(保留小數點后4位,並不一定與圓周率真值吻合)。 double x = 111; for(int n = 10000; n>=0; n--){ int i = 2 * n + 1; x = 2 + (i*i / x); } System.out.println(String.format("%.4f", ______________)); 4 / (x - 1)
6 奇怪的分式
標題:奇怪的分式 上小學的時候,小明經常自己發明新算法。一次,老師出的題目是: 1/4 乘以 8/5 小明居然把分子拼接在一起,分母拼接在一起,答案是:18/45 (參見圖1.png) 老師剛想批評他,轉念一想,這個答案湊巧也對啊,真是見鬼! 對於分子、分母都是 1~9 中的一位數的情況,還有哪些算式可以這樣計算呢? 請寫出所有不同算式的個數(包括題中舉例的)。 顯然,交換分子分母后,例如:4/1 乘以 5/8 是滿足要求的,這算做不同的算式。 但對於分子分母相同的情況,2/2 乘以 3/3 這樣的類型太多了,不在計數之列! 注意:答案是個整數(考慮對稱性,肯定是偶數)。請通過瀏覽器提交。不要書寫多余的內容。 14
public class Main { public static int count = 0; public void printResult() { for(int a = 1;a < 10;a++) { for(int b = 1;b < 10;b++) { if(b == a) continue; for(int c = 1;c < 10;c++) { for(int d = 1;d < 10;d++) { if(d == c) continue; int temp1 = b * d * (a * 10 + c); int temp2 = (b * 10 + d) * a * c; if(temp1 == temp2) count++; } } } } System.out.println(count); } public static void main(String[] args) { Main test = new Main(); test.printResult(); } }
7 撲克序列
標題:撲克序列 A A 2 2 3 3 4 4, 一共4對撲克牌。請你把它們排成一行。 要求:兩個A中間有1張牌,兩個2之間有2張牌,兩個3之間有3張牌,兩個4之間有4張牌。 請填寫出所有符合要求的排列中,字典序最小的那個。 例如:22AA3344 比 A2A23344 字典序小。當然,它們都不是滿足要求的答案。 請通過瀏覽器提交答案。“A”一定不要用小寫字母a,也不要用“1”代替。字符間一定不要留空格。 2342A3A4
public class Main { public static String result = "AAAAAAAA"; public void swap(char[] A, int a, int b) { char temp = A[a]; A[a] = A[b]; A[b] = temp; } public void dfs(char[] arrayA, int step) { if(step == arrayA.length) { getResult(arrayA); return; } else { for(int i = step;i < arrayA.length;i++) { swap(arrayA, i, step); dfs(arrayA, step + 1); swap(arrayA, i, step); } } return; } public int getSub(char[] arrayA, char temp) { int a1 = 0, a2 = 0; int i = 0; for(;i < arrayA.length;i++) { if(arrayA[i] == temp) { a1 = i; break; } } i++; for(;i < arrayA.length;i++) { if(arrayA[i] == temp) { a2 = i; break; } } return (a2 - a1); } public void getResult(char[] arrayA) { char temp1 = 'A', temp2 = '2', temp3 = '3', temp4 = '4'; boolean judge = false; int a = getSub(arrayA, temp1); int b = getSub(arrayA, temp2); int c = getSub(arrayA, temp3); int d = getSub(arrayA, temp4); if(a == 2 && b == 3 && c == 4 && d == 5) judge = true; if(judge == false) return; String A = ""; for(int i = 0;i < arrayA.length;i++) A += arrayA[i]; if(result.compareTo(A) > 0) result = A; return; } public static void main(String[] args) { Main test = new Main(); String A = "AA223344"; char[] arrayA = A.toCharArray(); test.dfs(arrayA, 0); System.out.println(result); } }
8 分糖果
標題:分糖果 有n個小朋友圍坐成一圈。老師給每個小朋友隨機發偶數個糖果,然后進行下面的游戲: 每個小朋友都把自己的糖果分一半給左手邊的孩子。 一輪分糖后,擁有奇數顆糖的孩子由老師補給1個糖果,從而變成偶數。 反復進行這個游戲,直到所有小朋友的糖果數都相同為止。 你的任務是預測在已知的初始糖果情形下,老師一共需要補發多少個糖果。 【格式要求】 程序首先讀入一個整數N(2<N<100),表示小朋友的人數。 接着是一行用空格分開的N個偶數(每個偶數不大於1000,不小於2) 要求程序輸出一個整數,表示老師需要補發的糖果數。 例如:輸入 3 2 2 4 程序應該輸出: 4 資源約定: 峰值內存消耗(含虛擬機) < 256M CPU消耗 < 1000ms 請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。 所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。 注意:不要使用package語句。不要使用jdk1.7及以上版本的特性。 注意:主類的名字必須是:Main,否則按無效代碼處理。
import java.util.Scanner; public class Main { public static long count = 0; public boolean judge(int[] A) { for(int i = 1;i < A.length;i++) { if(A[i - 1] == A[i]) continue; else return false; } return true; } public void getResult(int[] A) { int[] tempA = new int[A.length]; while(true) { for(int i = 0;i < A.length;i++) tempA[i] = A[i] / 2; A[0] = A[0] - tempA[0] + tempA[A.length - 1]; for(int i = 1;i < A.length;i++) A[i] = A[i] - tempA[i] + tempA[i - 1]; for(int i = 0;i < A.length;i++) { if(A[i] % 2 == 1) { count++; A[i] += 1; } } if(judge(A) == true) break; } System.out.println(count); return; } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); int n = in.nextInt(); int[] A = new int[n]; for(int i = 0;i < n;i++) A[i] = in.nextInt(); test.getResult(A); } }
9 地宮取寶
標題:地宮取寶 X 國王有一個地宮寶庫。是 n x m 個格子的矩陣。每個格子放一件寶貝。每個寶貝貼着價值標簽。 地宮的入口在左上角,出口在右下角。 小明被帶到地宮的入口,國王要求他只能向右或向下行走。 走過某個格子時,如果那個格子中的寶貝價值比小明手中任意寶貝價值都大,小明就可以拿起它(當然,也可以不拿)。 當小明走到出口時,如果他手中的寶貝恰好是k件,則這些寶貝就可以送給小明。 請你幫小明算一算,在給定的局面下,他有多少種不同的行動方案能獲得這k件寶貝。 【數據格式】 輸入一行3個整數,用空格分開:n m k (1<=n,m<=50, 1<=k<=12) 接下來有 n 行數據,每行有 m 個整數 Ci (0<=Ci<=12)代表這個格子上的寶物的價值 要求輸出一個整數,表示正好取k個寶貝的行動方案數。該數字可能很大,輸出它對 1000000007 取模的結果。 例如,輸入: 2 2 2 1 2 2 1 程序應該輸出: 2 再例如,輸入: 2 3 2 1 2 3 2 1 5 程序應該輸出: 14 資源約定: 峰值內存消耗(含虛擬機) < 256M CPU消耗 < 2000ms 請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。 所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。 注意:不要使用package語句。不要使用jdk1.7及以上版本的特性。 注意:主類的名字必須是:Main,否則按無效代碼處理。
import java.util.ArrayList; import java.util.Scanner; public class Main { public static long count = 0; public static int[][] move = {{0,1},{1,0}}; //表示分別向右、下移動一步 static class point { public int x; public int y; public ArrayList<Integer> list; point(int x, int y, ArrayList<Integer> list) { this.x = x; this.y = y; this.list = list; } } //獲取C(n , m)值 public long getCnm(int n, int m) { if(m == 0 || n == m) return 1; long result = 0; long temp1 = 1, temp2 = 1; for(int i = 0;i < m;i++) temp1 *= (n - i); for(int i = 1;i <= m;i++) temp2 *= i; result = temp1 / temp2; return result; } public boolean check(int[][] A, int x, int y) { if(x > A.length - 1 || y > A[0].length - 1) return false; return true; } public void bfs(int[][] A, int x, int y, int k) { ArrayList<point> list = new ArrayList<point>(); ArrayList<Integer> listV = new ArrayList<Integer>(); listV.add(A[x][y]); list.add(new point(x, y, listV)); while(list.size() > 0) { point temp = list.get(0); int tempX = temp.x; int tempY = temp.y; ArrayList<Integer> templistV = temp.list; list.remove(0); if(tempX == A.length - 1 && tempY == A[0].length - 1) { getResult(templistV, k); continue; } for(int i = 0;i < 2;i++) { int tempX1 = tempX + move[i][0]; int tempY1 = tempY + move[i][1]; if(check(A, tempX1, tempY1)) { ArrayList<Integer> templistV1 = new ArrayList<Integer>(); for(int j = 0;j < templistV.size();j++) templistV1.add(templistV.get(j)); templistV1.add(A[tempX1][tempY1]); list.add(new point(tempX1, tempY1, templistV1)); } } } } public void getResult(ArrayList<Integer> listV, int k) { int len = listV.size(); for(int i = 0;i <= len - k;i++) { int n = 1; for(int j = i + 1;j < len;j++) { if(listV.get(j) > listV.get(i)) n++; } if(n >= k) { //此處使用n-1,k-1。是因為當第i個元素必須要選擇 count = (count + getCnm(n - 1, k - 1)) % 1000000007; } } return; } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); int n = in.nextInt(); int m = in.nextInt(); int k = in.nextInt(); int[][] A = new int[n][m]; for(int i = 0;i < n;i++) for(int j = 0;j < m;j++) A[i][j] = in.nextInt(); test.bfs(A, 0, 0, k); System.out.println(count); } }
10 矩陣翻硬幣
標題:矩陣翻硬幣 小明先把硬幣擺成了一個 n 行 m 列的矩陣。 隨后,小明對每一個硬幣分別進行一次 Q 操作。 對第x行第y列的硬幣進行 Q 操作的定義:將所有第 i*x 行,第 j*y 列的硬幣進行翻轉。 其中i和j為任意使操作可行的正整數,行號和列號都是從1開始。 當小明對所有硬幣都進行了一次 Q 操作后,他發現了一個奇跡——所有硬幣均為正面朝上。 小明想知道最開始有多少枚硬幣是反面朝上的。於是,他向他的好朋友小M尋求幫助。 聰明的小M告訴小明,只需要對所有硬幣再進行一次Q操作,即可恢復到最開始的狀態。然而小明很懶,不願意照做。於是小明希望你給出他更好的方法。幫他計算出答案。 【數據格式】 輸入數據包含一行,兩個正整數 n m,含義見題目描述。 輸出一個正整數,表示最開始有多少枚硬幣是反面朝上的。 【樣例輸入】 2 3 【樣例輸出】 1 【數據規模】 對於10%的數據,n、m <= 10^3; 對於20%的數據,n、m <= 10^7; 對於40%的數據,n、m <= 10^15; 對於10%的數據,n、m <= 10^1000(10的1000次方)。 資源約定: 峰值內存消耗(含虛擬機) < 256M CPU消耗 < 2000ms 請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。 所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。 注意:不要使用package語句。不要使用jdk1.7及以上版本的特性。 注意:主類的名字必須是:Main,否則按無效代碼處理。 根據網上資料參考,要解決本題大數據輸入問題,使用大整數類型,且輸出結果滿足以下公式:f(x) =(n^1/2) * (m^1/2),兩者的開平方四舍五入的整數相乘。 下面代碼,使用暴力求解,對於小於int類型最大值的數據符合,代碼僅供參考。
import java.util.Scanner; public class Main { public static long n = 0; public static long m = 0; public boolean check(long x, long y) { if(x < 1 || x > n || y < 1 || y > m) return false; return true; } public void getResult(long[][] value) { for(long i = 1;i <= n;i++) { for(long j = 1;j <= m;j++) { for(long x = 1;x <= n/i;x++) { for(long y = 1;y <= m/j;y++) { long tempX = x * i; long tempY = y * j; if(check(tempX, tempY)) { value[(int) tempX][(int) tempY] = (value[(int) tempX][(int) tempY] + 1) % 2; } } } } } long count = 0; for(long i = 1;i <= n;i++) { for(long j = 1;j <= m;j++) { if(value[(int) i][(int) j] == 1) count++; } } System.out.println(count); return; } public static void main(String[] args) { Main test = new Main(); Scanner in = new Scanner(System.in); n = in.nextLong(); m = in.nextLong(); long[][] value = new long[(int) (n + 1)][(int) (m + 1)]; test.getResult(value); } }