藍橋杯算法訓練

一、最小乘積(基本型)【這個題需要認真閱讀試題,內容量較大,剛開始的時候,由於練習系統上給出的輸入輸出的格式有問題,沒看懂,最後在MikCu的博客上看到了正確的格式,參考了代碼,最終得到正確的結果。爲了讓結果最小,可以先分別對兩組數進行排序,然後對其中的一組數據逆序,逆序後,把兩組數據最大的與最小的相乘,最後求得的和最小!

問題描述
  給兩組數,各n個。
  請調整每組數的排列順序,使得兩組數據相同下標元素對應相乘,然後相加的和最小。要求程序輸出這個最小值。
  例如兩組數分別爲:1 3  -5和-2 4 1

  那麼對應乘積取和的最小值應爲:
  (-5) * 4 + 3 * (-2) + 1 * 1 = -25
輸入格式
  第一個行一個數T表示數據組數。後面每組數據,先讀入一個n,接下來兩行每行n個數,每個數的絕對值小於等於1000。
  n<=8,T<=1000
輸出格式
  一個數表示答案。
樣例輸入
  2    //兩組數據
  3    //每行三個數
  1 3 -5
  -2 4 1
  5    //每行五個數
  1 2 3 4 5
  1 0 1 0 1
樣例輸出
  -25
  6
複製代碼
 1 import java.util.Arrays;
 2 import java.util.Scanner;
 3 
 4 public class Main {
 5     static Scanner mScanner;
 6 
 7     public static void main(String[] args) {
 8 
 9         mScanner = new Scanner(System.in);
10         int n = mScanner.nextInt();
11         
12         int[] sum = new int[n];
13         for (int i = 0; i < n; i++) {
14             sum[i] = getMaxSum(); 
15         }
16         for (int i = 0; i < n; i++) {
17             System.out.println(sum[i]);
18         }
19 
20     }
21 
22     public static int getMaxSum() {
23         int count = mScanner.nextInt();
24         int[] array1 = new int[count];
25         int[] array2 = new int[count];
26         for (int i = 0; i < count; i++) {
27             array1[i] = mScanner.nextInt();
28         }
29         for (int j = 0; j < count; j++) {
30             array2[j] = mScanner.nextInt();
31         }
32         Arrays.sort(array1);
33         Arrays.sort(array2);
34         array2 = getReverse(array2);
35         
36         int sum = 0;
37         for (int i = 0; i < count; i++) {
38             sum = array1[i] * array2[i] + sum;
39         }
40 
41         return sum;
42     }
43 
44     public static int[] getReverse(int[] arr) {
45         int length = arr.length;
46         int[] array = new int[length];
47         for (int i = 0; i < arr.length; i++) {
48             array[i] = arr[length - 1];
49             length--;
50         }
51 
52         return array;
53     }
54 
55 }
複製代碼

 

二、Torry的困惑(基本型)【此題的關鍵是保存素數的積對50000取餘的結果,如果保留乘積的話,會造成數據過大,內存溢出等問題……】

問題描述
  Torry從小喜愛數學。一天,老師告訴他,像2、3、5、7……這樣的數叫做質數。Torry突然想到一個問題,前10、100、1000、10000……個質數的乘積是多少呢?他把這個問題告訴老師。老師愣住了,一時回答不出來。於是Torry求助於會編程的你,請你算出前n個質數的乘積。不過,考慮到你才接觸編程不久,Torry只要你算出這個數模上50000的值。
輸入格式
  僅包含一個正整數n,其中n<=100000。
輸出格式
  輸出一行,即前n個質數的乘積模50000的值。
樣例輸入
  1
樣例輸出
  2
複製代碼
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6 
 7         Scanner mScanner = new Scanner(System.in);
 8         int n = mScanner.nextInt();
 9         int i = 0;
10         int start = 2;
11         int ss = 1;
12         while(i<n){
13             if (isSushu(start)) {
14                 ss = (ss*start)%50000;
15                 i++;
16             }
17             start++;
18         }
19         System.out.println(ss);
20     }
21     
22     public static boolean isSushu(int m){
23         for (int i = 2; i*i <= m; i++) {
24             if (m%i==0) {
25                 return false;
26             }
27         }
28         return true;
29         
30     }
31 
32 }
複製代碼

 

三、尋找數組中最大值【此題求最大值不能使用sort()函數了,因爲要輸出其下標,所以此處使用的是循環,不過我剛剛想到了一個方法,就是先使用sort函數對原數組的備份進行排序,然後得到最大值,在原數組上使用getIndex()方法得到其索引,此方法應該可行。】

問題描述
  對於給定整數數組a[],尋找其中最大值,並返回下標。
輸入格式
  整數數組a[],數組元素個數小於1等於100。輸出數據分作兩行:第一行只有一個數,表示數組元素個數;第二行爲數組的各個元素。
輸出格式
  輸出最大值,及其下標
樣例輸入
  3
  3 2 1
樣例輸出
  3 0
複製代碼
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4 
 5     public static void main(String[] args) {
 6 
 7         Scanner mScanner = new Scanner(System.in);
 8         int n = mScanner.nextInt();
 9         int[] arrays = new int[n];
10         for (int i = 0; i < n; i++) {
11             arrays[i] = mScanner.nextInt();
12         }
13 
14         int max = Integer.MIN_VALUE;
15         int maxIndex =0;
16         for (int i = 0; i < arrays.length; i++) {
17             if (arrays[i] > max) {
18                 max = arrays[i];
19                 maxIndex = i;
20             }
21         }
22         
23         System.out.println(max+" "+maxIndex);
24 
25     }
26 
27 }
複製代碼

 

四、關聯矩陣【】

問題描述
  有一個n個結點m條邊的有向圖,請輸出他的關聯矩陣。
輸入格式
  第一行兩個整數n、m,表示圖中結點和邊的數目。n<=100,m<=1000。
  接下來m行,每行兩個整數a、b,表示圖中有(a,b)邊。
  注意圖中可能含有重邊,但不會有自環。
輸出格式
  輸出該圖的關聯矩陣,注意請勿改變邊和結點的順序。
樣例輸入
  5 9
  1 2
  3 1
  1 5
  2 5
  2 3
  2 3 
  3 2
  4 3
  5 4
樣例輸出
  1 -1 1 0 0 0 0 0 0
  -1 0 0 1 1 1 -1 0 0
  0 1 0 0 -1 -1 1 -1 0
  0 0 0 0 0 0 0 1 -1
  0 0 -1 -1 0 0 0 0 1
複製代碼
 1 import java.util.Scanner;
 2 
 3 public class Main{
 4 
 5     public static void main(String[] args) {
 6 
 7         Scanner mScanner = new Scanner(System.in);
 8         int n = mScanner.nextInt();//dian
 9         int m = mScanner.nextInt();//bian
10         int[][] array = new int[n][m];
11         int x,y;
12         for (int i = 0; i < m; i++) {
13             x = mScanner.nextInt();
14             y = mScanner.nextInt();
15             array[x-1][i] =1;
16             array[y-1][i] =-1;
17         }
18         for (int i = 0; i < n; i++) {
19             for (int j = 0; j < m; j++) {
20                 System.out.print(array[i][j]+" ");
21             }
22             System.out.println();
23         }
24         
25     }
26 
27 }
複製代碼
發佈了52 篇原創文章 · 獲贊 61 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章