(摘錄筆記)藍橋杯算法訓練

一、圖形顯示【此題雖然簡單,但是需啊喲注意的是,每個“*”後邊有一個空格】

問題描述

  編寫一個程序,首先輸入一個整數,例如5,然後在屏幕上顯示如下的圖形(5表示行數):
  * * * * *
  * * * *
  * * *
  * *
  *

複製代碼

 1 import java.util.Scanner; 2  3 public class Main { 4     public static void main(String[] args) { 5         Scanner mScanner = new Scanner(System.in); 6         int n = mScanner.nextInt(); 7         for (int i = n; i > 0; i--) { 8             if (i != n) { 9                 System.out.println();10             }11             for (int j = 0; j < i; j++) {12                 System.out.print("* ");13             }14         }15     }16 }

複製代碼

 

二、排序【由小到大排序後逆序輸出】

問題描述

  編寫一個程序,輸入3個整數,然後程序將對這三個整數按照從大到小進行排列。
  輸入格式:輸入只有一行,即三個整數,中間用空格隔開。
  輸出格式:輸出只有一行,即排序後的結果。
  輸入輸出樣例

樣例輸入

  9 2 30

樣例輸出

  30 9 2

 

複製代碼

 1 import java.util.Arrays; 2 import java.util.Scanner; 3  4 public class Main{ 5  6     public static void main(String[] args) { 7         Scanner mScanner = new Scanner(System.in); 8         int[] array = new int[3]; 9         for (int i = 0; i < array.length; i++) {10             array[i] = mScanner.nextInt();11         }12         Arrays.sort(array);13         for (int i = array.length - 1; i >= 0; i--) {14             System.out.print(array[i] + " ");15         }16     }17 18 }

複製代碼

 

三、2的次冪表示【這個是從網上找的一個答案,通過測試】

問題描述

  任何一個正整數都可以用2進製表示,例如:137的2進製表示爲10001001。
  將這種2進製表示寫成2的次冪的和的形式,令次冪高的排在前面,可得到如下表達式:137=2^7+2^3+2^0
  現在約定冪次用括號來表示,即a^b表示爲a(b)
  此時,137可表示爲:2(7)+2(3)+2(0)
  進一步:7=2^2+2+2^0 (2^1用2表示)
  3=2+2^0 
  所以最後137可表示爲:2(2(2)+2+2(0))+2(2+2(0))+2(0)
  又如:1315=2^10+2^8+2^5+2+1
  所以1315最後可表示爲:
  2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)

輸入格式

  正整數(1<=n<=20000)

輸出格式

  符合約定的n的0,2表示(在表示中不能有空格)

樣例輸入

  137

樣例輸出

  2(2(2)+2+2(0))+2(2+2(0))+2(0)

樣例輸入

  1315

樣例輸出

  2(2(2+2(0))+2)+2(2(2+2(0)))+2(2(2)+2(0))+2+2(0)
提示
  用遞歸實現會比較簡單,可以一邊遞歸一邊輸出

複製代碼

 1 import java.util.Scanner; 2  3 public class Main{ 4     public void fun(double e) { 5         double d = e; 6         if (d == 1) { 7             System.out.print("2(0)"); 8         } else { 9             if (d == 2) {10                 System.out.print("2");11             } else {12                 int x = 1;13                 int m = 0;14                 while (m == 0) {15                     if ((Math.pow(2, x + 1) > d) && (d >= Math.pow(2, x))) {16                         m = 1;17                         if (d == Math.pow(2, x)) {18                             System.out.print("2");19                             if (x == 1) {20                             } else {21                                 System.out.print("(");22                                 fun(x);23                                 System.out.print(")");24                             }25                         } else {26                             System.out.print("2");27                             if (x == 1) {28                             } else {29                                 System.out.print("(");30                                 fun(x);31                                 System.out.print(")");32                             }33                             System.out.print("+");34                             fun(d - Math.pow(2, x));35                         }36                     } else {37                         x++;38                     }39                 }40             }41         }42     }43 44     public static void main(String[] args) {45         Scanner mScanner = new Scanner(System.in);46         double n = mScanner.nextInt();47         Main cm = new Main();48         cm.fun(n);49     }50 }

複製代碼

 

四、前綴表達式【此題貌似沒有按照題中要求的設計相應的函數,但是也通過了測試】

問題描述

  編寫一個程序,以字符串方式輸入一個前綴表達式,然後計算它的值。輸入格式爲:“運算符 對象1 對象2”,其中,運算符爲“+”(加法)、“-”(減法)、“*”(乘法)或“/”(除法),運算對象爲不超過10的整數,它們之間用一個空格隔開。要求:對於加、減、乘、除這四種運算,分別設計相應的函數來實現。
  輸入格式:輸入只有一行,即一個前綴表達式字符串。
  輸出格式:輸出相應的計算結果(如果是除法,直接採用c語言的“/”運算符,結果爲整數)。
  輸入輸出樣例

樣例輸入

  + 5 2

樣例輸出

  7

複製代碼

 1 import java.util.Scanner; 2  3 public class Main { 4      5     public static void main(String[] args) { 6         Scanner mScanner = new Scanner(System.in); 7         String mm = mScanner.next(); 8         int a = mScanner.nextInt(); 9         int b = mScanner.nextInt();10         if (mm.equals("+")) {11             System.out.println(a+b);12         }else if (mm.equals("-")) {13             System.out.println(a-b);14         }else if (mm.equals("*")) {15             System.out.println(a*b);16         }else if (mm.equals("/")) {17             System.out.println(a/b);18         }19     }20 }

複製代碼

 

五、Anagrams問題【我的做法是先把輸入的字符串使用 toLowerCase() 函數全部轉化爲小寫字母,然後使用排序函數對字符數組進行排序,最後依次比較每個字符的大小即可!】

問題描述

  Anagrams指的是具有如下特性的兩個單詞:在這兩個單詞當中,每一個英文字母(不區分大小寫)所出現的次數都是相同的。例如,“Unclear”和“Nuclear”、“Rimon”和“MinOR”都是Anagrams。編寫一個程序,輸入兩個單詞,然後判斷一下,這兩個單詞是否是Anagrams。每一個單詞的長度不會超過80個字符,而且是大小寫無關的。
  輸入格式:輸入有兩行,分別爲兩個單詞。
  輸出格式:輸出只有一個字母Y或N,分別表示Yes和No。
  輸入輸出樣例

樣例輸入

  Unclear
  Nuclear

樣例輸出

  Y

複製代碼

 1 import java.util.Arrays; 2 import java.util.Scanner; 3  4 public class Main{ 5  6     public static void main(String[] args) { 7         Scanner mScanner = new Scanner(System.in); 8         String str1 = mScanner.next().toLowerCase(); 9         String str2 = mScanner.next().toLowerCase();10         if (str1.length() == str2.length()) {11             char[] arr1 = fun(str1);12             char[] arr2 = fun(str2);13             boolean tag = true;14             for (int i = 0; i < arr1.length; i++) {15                 if (arr1[i] != arr2[i]) {16                     tag = false;17                 }18             }19             if (tag) {20                 System.out.println("Y");21             }else{22                 System.out.println("N");23             }24         } else {25             System.out.println("N");26         }27     }28 29     public static char[] fun(String str) {30         char[] array = str.toCharArray();31         Arrays.sort(array);32         return array;33     }34 35 }

複製代碼

 

六、出現次數最多的整數【此題沒有太大難度,但是如果考慮的不全面的話想得100分也有點難度,問題是他給出的測試的數據中給出的測試數值小於或者等於1,大於20……這就是一個坑。。。】

問題描述
  編寫一個程序,讀入一組整數,這組整數是按照從小到大的順序排列的,它們的個數N也是由用戶輸入的,最多不會超過20。然後程序將對這個數組進行統計,把出現次數最多的那個數組元素值打印出來。如果有兩個元素值出現的次數相同,即並列第一,那麼只打印比較小的那個值。
  輸入格式:第一行是一個整數N,N   £  20;接下來有N行,每一行表示一個整數,並且按照從小到大的順序排列。
  輸出格式:輸出只有一行,即出現次數最多的那個元素值。
  輸入輸出樣例

樣例輸入

  5
  100
  150
  150
  200
  250

樣例輸出

  150

複製代碼

 1 import java.util.Scanner; 2  3 public class Main{ 4  5     public static void main(String[] args) { 6         Scanner mScanner = new Scanner(System.in); 7         int m = mScanner.nextInt(); 8         if (m < 1 || m > 20) { 9             return;10         }11         int[] array = new int[m];12         for (int i = 0; i < m; i++) {13             array[i] = mScanner.nextInt();14         }15 16         int max = 0;17         int tag = 0;18         int num = 0;19         for (int i = 1; i < array.length; i++) {20             if (array[i - 1] == array[i]) {21                 tag++;22             } else {23                 tag = 0;24             }25             if (tag > max) {26                 num = array[i];27                 max = tag;28             }29         }30         if (num==0) {31             num = array[0];32         }33         System.out.println(num);34     }35 36 }

複製代碼

 

七、字串統計

問題描述

  給定一個長度爲n的字符串S,還有一個數字L,統計長度大於等於L的出現次數最多的子串(不同的出現可以相交),如果有多個,輸出最長的,如果仍然有多個,輸出第一次出現最早的。

輸入格式

  第一行一個數字L。
  第二行是字符串S。
  L大於0,且不超過S的長度。

輸出格式

  一行,題目要求的字符串。

  輸入樣例1:
  4
  bbaabbaaaaa

  輸出樣例1:
  bbaa

  輸入樣例2:
  2
  bbaabbaaaaa

  輸出樣例2:
  aa

數據規模和約定

  n<=60
  S中所有字符都是小寫英文字母。

  提示
  枚舉所有可能的子串,統計出現次數,找出符合條件的那個

 

 

八、矩陣乘法【此題考查矩陣的乘法,如果不會矩陣的乘法的話,可以好好看看這個題,還有二維數組的運算問題】

問題描述

  輸入兩個矩陣,分別是m*s,s*n大小。輸出兩個矩陣相乘的結果。

輸入格式

  第一行,空格隔開的三個正整數m,s,n(均不超過200)。
  接下來m行,每行s個空格隔開的整數,表示矩陣A(i,j)。
  接下來s行,每行n個空格隔開的整數,表示矩陣B(i,j)。

輸出格式

  m行,每行n個空格隔開的整數,輸出相乘後的矩陣C(i,j)的值。

樣例輸入

  2 3 2
  1 0 -1
  1 1 -3
  0 3
  1 2
  3 1

樣例輸出

  -3 2
  -8 2

提示
  矩陣C應該是m行n列,其中C(i,j)等於矩陣A第i行行向量與矩陣B第j列列向量的內積。
  例如樣例中C(1,1)=(1,0,-1)*(0,1,3) = 1 * 0 +0*1+(-1)*3=-3

複製代碼

 1 import java.util.Scanner; 2  3  4 public class Main { 5  6     public static void main(String[] args) { 7         Scanner mScanner = new Scanner(System.in); 8         int m = mScanner.nextInt(); 9         int s = mScanner.nextInt();10         int n = mScanner.nextInt();11         int[][] a = new int[m][s];12         int[][] b = new int[s][n];13         int[][] c = new int[m][n];14         for (int i = 0; i < m; i++) {15             for (int j = 0; j < s; j++) {16                 a[i][j] = mScanner.nextInt();17             }18         }19         for (int i = 0; i < s; i++) {20             for (int j = 0; j < n; j++) {21                 b[i][j] = mScanner.nextInt();22             }23         }24         25         getMatrix(a, b);26         27     }28 29     public static void getMatrix(int[][] a, int[][] b) {30         int[][] c = new int[a.length][b[0].length];31         for (int i = 0; i < a.length; i++) {32             for (int j = 0; j < b[0].length; j++) {33                 int sum = 0;34                 for (int k = 0; k < b.length; k++) {35                     sum += a[i][k] * b[k][j];36                 }37                 c[i][j] = sum;38             }39         }40 41         for (int i = 0; i < a.length; i++) {42             for (int j = 0; j < b[0].length; j++) {43                 System.out.print(c[i][j] + "\t");44             }45             System.out.println();46         }47 48     }49 50 }

複製代碼

 

九、大小寫轉換大小寫轉換函數,字符串到字符數組的轉化函數

問題描述

  編寫一個程序,輸入一個字符串(長度不超過20),然後把這個字符串內的每一個字符進行大小寫變換,即將大寫字母變成小寫,小寫字母變成大寫,然後把這個新的字符串輸出。
  輸入格式:輸入一個字符串,而且這個字符串當中只包含英文字母,不包含其他類型的字符,也沒有空格。
  輸出格式:輸出經過轉換後的字符串。
  輸入輸出樣例

樣例輸入

  AeDb

樣例輸出

  aEdB

複製代碼

 1 import java.util.Scanner; 2 public class Main { 3  4     public static void main(String[] args) { 5         Scanner mScanner = new Scanner(System.in); 6         String str = mScanner.next(); 7         char[] arr = str.toCharArray(); 8         for (int i = 0; i < arr.length; i++) { 9             if (Character.isLowerCase(arr[i])) {10                 str = String.valueOf(arr[i]).toUpperCase();11             } else {12                 str = String.valueOf(arr[i]).toLowerCase();13             }14             System.out.print(str);15         }16     }17 18 }

複製代碼

 

十、動態數組使用【此題可以考慮使用arrayList動態鏈表!】

 從鍵盤讀入n個整數,使用動態數組存儲所讀入的整數,並計算它們的和與平均值分別輸出。要求儘可能使用函數實現程序代碼。平均值爲小數的只保留其整數部分。

樣例輸入

  5
  3 4 0 0 2

樣例輸出

  9 1

樣例輸入

  7
  3 2 7 5 2 9 1

樣例輸出

  29 4

複製代碼

 1 import java.util.ArrayList; 2 import java.util.Scanner; 3  4 public class Main { 5  6     public static void main(String[] args) { 7         Scanner mScanner = new Scanner(System.in); 8         int length = mScanner.nextInt(); 9 10         int sum = 0;11         int average = 0;12         for (int i = 0; i < length; i++) {13             sum = sum + mScanner.nextInt();14         }15         System.out.println(sum + " " + sum / length);16 17     }18 19 }

複製代碼

 

十一、刪除數組零元素【此題如果使用數組的話可能比較麻煩,但是使用 ArrayList 的話就能達到事半功倍的效果

  從鍵盤讀入n個整數放入數組中,編寫函數CompactIntegers,刪除數組中所有值爲0的元素,其後元素向數組首端移動。注意,CompactIntegers函數需要接受數組及其元素個數作爲參數,函數返回值應爲刪除操作執行後數組的新元素個數。輸出刪除後數組中元素的個數並依次輸出數組元素。  樣例輸入: (輸入格式說明:5爲輸入數據的個數,3 4 0 0 2 是以空格隔開的5個整數)
  5
  3 4 0 0 2
  樣例輸出:(輸出格式說明:3爲非零數據的個數,3 4 2 是以空格隔開的3個非零整數)
  3
  3 4 2

樣例輸入

  7
  0 0 7 0 0 9 0

樣例輸出

  2
  7 9

樣例輸入

  3
  0 0 0

樣例輸出

  0

複製代碼

 1 import java.util.ArrayList; 2 import java.util.List; 3 import java.util.Scanner; 4  5  6 public class Main { 7  8     public static void main(String[] args) { 9 10         Scanner mScanner = new Scanner(System.in);11         int count = mScanner.nextInt();12 13         List<Integer> list = new ArrayList<Integer>();14 15         // int[] arr = new int[count];16         for (int i = 0; i < count; i++) {17             // arr[i] = mScanner.nextInt();18             list.add(mScanner.nextInt());19         }20         int length = CompactIntegers(list, count);21         System.out.println(length);22 23         for (int i = 0; i < list.size(); i++) {24             System.out.print(list.get(i) + " ");25         }26 27     }28 29     public static int CompactIntegers(List<Integer> list, int count) {30         int ss = count;31         for (int i = 0; i < list.size(); i++) {32             if (list.get(i) == 0) {33                 list.remove(i);34                 ss--;35                 i--;36             }37         }38         return ss;39     }40 }

複製代碼

文章摘自:http://www.cnblogs.com/zhjsll/p/4412250.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章