算法筆記_216:第六屆藍橋杯軟件類校賽部分真題(Java語言C組)


目錄

1 題目一

2 題目二

3 題目三

4 題目四

5 題目五

 

 

 

 前言:以下代碼僅供參考,若有錯誤歡迎指正哦~


1 題目一

二項式的系數規律,我國數學家很早就發現了。

如【圖1.png】,我國南宋數學家楊輝1261年所著的《詳解九章算法》一書里就出現了。

其排列規律:
1
1    1
1    2    1
1    3    3    1
1    4    6    4    1
1    5    10   10   5    1
1    6    15   20   15   6    1
1    7    21   35   35   21   7    1

如下的程序,用來建立N行的楊輝三角形。請填寫划線部分缺少的代碼。

注意:只填寫划線部分的代碼,不要填寫任何多余的內容。



public class A
{
    public static void main(String[] args)
    {
        int N = 8;
        int[][] a = new int[N][N] ;
        
        for(int i=0; i<N; i++){
            a[i][0] = 1;
            a[i][i] = 1;
        }
        
        for(int i=1; i<N; i++){
            for(int j=1; j<i; j++) _____________________________;  //填空
        }
        
        for(int i=0; i<N; i++){
            for(int j=0; j<=i; j++)    System.out.print(String.format("%-5d", a[i][j]));
            System.out.println();
        }    
    }
}



答案:a[i][j] = a[i - 1][j - 1] + a[i - 1][j]

 

 

 

 


2 題目二

10301是個5位的素數。它有個特點,把數字倒過來還是它本身,具有這樣特征的素數,我們稱之為:回文素數。

10501
10601
11311

這些都是5位的回文素數。

請你計算一下,像這樣的5位數的回文素數,一共有多少個?

請填寫這個表示個數的整數,注意不要寫任何其它多余的內容,比如說明或解釋文字,也不要列出所有的回文素數。


答案:93

 

 1 public class Main {
 2     
 3     public boolean judgePrime(int n) {
 4         boolean judge = true;
 5         for(int i = 2;i <= n / 2;i++)
 6             if(n % i == 0) {
 7                 judge = false;
 8                 break;
 9             }
10         return judge;
11     }
12     
13     public boolean judgeReverse(int n) {
14         StringBuffer s = new StringBuffer(""+n);
15         String s1 = "", s2 = "";
16         s1 = s.toString();
17         s2 = s.reverse().toString();
18         if(s1.equals(s2))
19             return true;
20         else
21             return false;
22     }
23     public static void main(String[] args) {
24         Main test = new Main();
25         int count = 0;
26         for(int i = 10000;i < 100000;i++) {
27             if(test.judgePrime(i) && test.judgeReverse(i)) {
28                 count++;
29                 System.out.println(i);
30             }
31         }
32         System.out.println("結果:"+count);
33     }
34 }

 

 

 

 

 


3 題目三

有如下的加法算式。其中每個漢字代表一個數字。
(如存在對齊問題,可參見【圖1.png】)

               年
             大年
           過大年
         能過大年
       怎能過大年
     我怎能過大年
+  讓我怎能過大年
------------------
   能能能能能能能

請填寫“讓我怎能過大年” 所代表的整數。
所有數字連在一起,中間不要空格。例如:"3125697"。當然,這個不是正確的答案。

注意:只填寫一個整數,不要填寫任何多余的內容。


答案:1572836

 1 public class Main {
 2     public static boolean[] used = new boolean[10];
 3     
 4     public void dfs(int[] A, int step) {
 5         if(step == 7) {
 6             int[] sum = new int[7];
 7             sum[0] = A[0]*1000000 + A[1]*100000 + A[2]*10000 + 
 8                  A[3]*1000 + A[4]*100 + A[5]*10 + A[6];
 9             sum[1] = sum[0] - A[0]*1000000;
10             sum[2] = sum[1] - A[1]*100000;
11             sum[3] = sum[2] - A[2]*10000;
12             sum[4] = sum[3] - A[3]*1000;
13             sum[5] = sum[4] - A[4]*100;
14             sum[6] = sum[5] - A[5]*10;
15             int judge1 = 0, judge2 = 0;
16             for(int i = 0;i < 7;i++) {
17                 judge1 = judge1 * 10 + A[3];
18                 judge2 = judge2 + sum[i];
19             }
20             if(judge1 == judge2) {
21                 for(int i = 0;i < 7;i++)
22                     System.out.print(A[i]);
23                 System.out.println();
24             }
25             return;
26         } else {
27             for(int i = 0;i <= 9;i++) {
28                 if(step == 0 && i == 0)
29                     continue;
30                 if(!used[i]) {
31                     used[i] = true;
32                     A[step] = i;
33                     dfs(A, step + 1);
34                     used[i] = false;
35                 }
36             }
37         }
38     }
39     
40     public static void main(String[] args) {
41         Main test = new Main();
42         int[] A = new int[7];
43         test.dfs(A, 0);
44     }
45 }

 

 

 

 

 


4 題目四

形如:1/a 的分數稱為單位分數。

可以把1分解為若干個互不相同的單位分數之和。
例如:
1 = 1/2 + 1/3 + 1/9 + 1/18
1 = 1/2 + 1/3 + 1/10 + 1/15
1 = 1/3 + 1/5 + 1/7 + 1/9 + 1/11 + 1/15 + 1/35 + 1/45 + 1/231
等等,類似這樣的分解無窮無盡。

我們增加一個約束條件:最大的分母必須不超過30

請你求出分解為n項時的所有不同分解法。

數據格式要求:

輸入一個整數n,表示要分解為n項(n<12)
輸出分解后的單位分數項,中間用一個空格分開。
每種分解法占用一行,行間的順序按照分母從小到大排序。

例如,
輸入:
4
程序應該輸出:
1/2 1/3 1/8 1/24
1/2 1/3 1/9 1/18
1/2 1/3 1/10 1/15
1/2 1/4 1/5 1/20
1/2 1/4 1/6 1/12

再例如,
輸入:
5
程序應該輸出:
1/2 1/3 1/12 1/21 1/28
1/2 1/4 1/6 1/21 1/28
1/2 1/4 1/7 1/14 1/28
1/2 1/4 1/8 1/12 1/24
1/2 1/4 1/9 1/12 1/18
1/2 1/4 1/10 1/12 1/15
1/2 1/5 1/6 1/12 1/20
1/3 1/4 1/5 1/6 1/20


資源約定:
峰值內存消耗(含虛擬機) < 256M
CPU消耗  < 2000ms


請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。

所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。
注意:不要使用package語句。不要使用jdk1.7及以上版本的特性。
注意:主類的名字必須是:Main,否則按無效代碼處理。
 1 import java.util.Scanner;
 2 
 3 public class Main {
 4     
 5     public static int n;
 6     public static long Lcd_1_30 = 2329089562800L; //1-30的最小公倍數
 7     public static int[] A;
 8     
 9     public void dfs(int step, int nowNum, long result) {
10         if(step == n) {
11             if(result != Lcd_1_30)
12                 return;
13             for(int i = 0;i < n;i++)
14                 System.out.print("1/"+A[i]+" ");
15             System.out.println();
16             return;
17         }
18         if(result > Lcd_1_30)
19             return;
20         for(int i = nowNum + 1;i < 30;i++) {
21             A[step] = i;
22             dfs(step + 1, i, result + Lcd_1_30 / i);
23         }
24     }
25     
26     public static void main(String[] args) {
27         Main test = new Main();
28         Scanner in = new Scanner(System.in);
29         n = in.nextInt();
30         A = new int[n];
31         test.dfs(0, 0, 0);
32     }
33 }

 參考資料:第五屆藍橋杯Java語言C組_單位分數

 

 

 


5 題目五

有n級台階。從地面(第0級)出發,首先連續的上台階,上到不超過第n級的某一個位置后再連續的下台階,直到回到地面。若每次上下台階只允許走1級或2級,請問可能的上下台階的方案數是多少?
特別地,在0級站着不動也算一種方案。

數據格式:

輸入一行包含兩個正整數n和m。
輸出一個整數,表示n級台階有多少種合法的走樓梯方案,答案對m取余。

例如:輸入:
2 10007
程序應該輸出
6

【樣例說明1】
共有6種方案(其中+表示上台階,-表示下台階):
(1) 原地不動
(2) +1 -1
(3) +2 -2
(4) +2 -1 -1
(5) +1 +1 -2
(6) +1 +1 -1 -1

再例如,輸入:
3 14
程序應該輸出:
1

【樣例說明2】
共有15種方案,對14取余后得1。

【數據規模】
對於30%的數據,n<=10000;
對於100%的數據,n<=10^17,m<=2*10^9。


資源約定:
峰值內存消耗(含虛擬機) < 256M
CPU消耗  < 1000ms


請嚴格按要求輸出,不要畫蛇添足地打印類似:“請您輸入...” 的多余內容。

所有代碼放在同一個源文件中,調試通過后,拷貝提交該源碼。
注意:不要使用package語句。不要使用jdk1.7及以上版本的特性。
注意:主類的名字必須是:Main,否則按無效代碼處理。
 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 
 4 
 5 public class Main {
 6     public static long n, m;
 7     public static BigInteger MOD;
 8     public static BigInteger zero = BigInteger.ZERO;
 9     public static BigInteger one = BigInteger.ONE;
10     public BigInteger[][] ZERO = {{zero,zero},{zero,zero}};
11     public BigInteger[][] key = {{one, one},{one, zero}};
12     
13     public BigInteger[][] mergeValue(long a) {
14         if(a == 0)
15             return ZERO;
16         if(a == 1)
17             return key;
18         if((a&1) == 0) {
19             BigInteger[][] temp = mergeValue(a>>1);
20             return multiMatrix(temp, temp);
21         } else {
22             BigInteger[][] temp = mergeValue(a>>1);
23             return multiMatrix(multiMatrix(temp, temp), key);
24         }
25     }
26     
27     public BigInteger[][] multiMatrix(BigInteger[][] A, BigInteger[][] B) {
28         BigInteger[][] result = new BigInteger[A.length][B[0].length];
29         for(int i = 0;i < result.length;i++)
30             for(int j = 0;j < result[0].length;j++)
31                 result[i][j] = zero;
32         for(int i = 0;i < A.length;i++)
33             for(int j = 0;j < B[0].length;j++)
34                 for(int k = 0;k < A[0].length;k++) {
35                     result[i][j] = result[i][j].add(A[i][k].multiply(B[k][j]));
36                     result[i][j] = result[i][j].mod(MOD);
37                 }
38         return result;    
39     }
40     
41     public void getResult() {
42         BigInteger result;
43         BigInteger[][] start = {{one, one}};
44         BigInteger[][] temp = multiMatrix(start, mergeValue(n));
45         result = temp[0][0].multiply(temp[0][1]);
46         System.out.println(result.mod(MOD));
47     }
48     
49     public static void main(String[] args) {
50         Main test = new Main();
51         Scanner in = new Scanner(System.in);
52         n = in.nextLong();
53         m = in.nextLong();
54         MOD = new BigInteger(""+m);
55         test.getResult();
56     }
57 }

 參考資料: N階台階

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM