藍橋杯 基礎練習02 Java實現

藍橋杯的入門練習,別處的答案有的寫得很冗餘,沒有充分利用Java簡潔的方法,在這裏有我自己整合寫的代碼,僅提供java實現,如有問題,歡迎探討。

首先,這部分題目是vip的了。想自己搭建檢測環境的可以參考我的博文:簡易實現藍橋檢測系統
本節代碼均在我寫的系統類的基礎上實現的。想自己運行復制fun方法內部的代碼,如果用到Scanner的對象,就在代碼最開始寫一行創建Scanner對象的代碼就可以了。

  • 時間轉換
/*  
基礎練習 時間轉換  

問題描述  
  給定一個以秒爲單位的時間t,要求用“<H>:<M>:<S>”的格式來表示這個時間。<H>表示時間,<M>表示分鐘,而<S>表示秒,它們都是整數且沒有前導的“0”。例如,若t=0,則應輸出是“0:0:0”;若t=3661,則輸出“1:1:1”。  
輸入格式  
  輸入只有一行,是一個整數t(0<=t<=86399)。  
輸出格式  
  輸出只有一行,是以“<H>:<M>:<S>”的格式所表示的時間,不包括引號。  
樣例輸入  
0  
樣例輸出  
0:0:0  
樣例輸入  
5436  
樣例輸出  
1:30:36  
*/
public class Main10 {

    public static void main(String[] args) {
        LT lt=new LT("時間轉換") {
            @Override
            public void fun() {
                int num=sc.nextInt();
                int h=0;
                int m=0;
                int s=0;
                h=num/3600; 
                num=num%3600;
                m=num/60;
                s=num%60;
                System.out.println(h+":"+m+":"+s);
            }
        };
        lt.run();
    }

}
  • 字符串對比
/*  
基礎練習 字符串對比  

問題描述  
  給定兩個僅由大寫字母或小寫字母組成的字符串(長度介於1到10之間),它們之間的關  

系是以下4中情況之一:  
  1:兩個字符串長度不等。比如 Beijing 和 Hebei  
  2:兩個字符串不僅長度相等,而且相應位置上的字符完全一致(區分大小寫),比如   

Beijing 和 Beijing  
  3:兩個字符串長度相等,相應位置上的字符僅在不區分大小寫的前提下才能達到完  

全一致(也就是說,它並不滿足情況2)。比如 beijing 和 BEIjing  
  4:兩個字符串長度相等,但是即使是不區分大小寫也不能使這兩個字符串一致。比  

如 Beijing 和 Nanjing  
  編程判斷輸入的兩個字符串之間的關係屬於這四類中的哪一類,給出所屬的類的編號  

。  
輸入格式  
  包括兩行,每行都是一個字符串  
輸出格式  
  僅有一個數字,表明這兩個字符串的關係編號  
樣例輸入  
BEIjing  
beiJing  
樣例輸出  
3  
*/ 
public class Main11 {

    public static void main(String[] args) {
        LT lt=new LT("字符串對比") {
            @Override
            public void fun() {
                String s1=sc.nextLine();
                String s2=sc.nextLine();
                if(s1.length()!=s2.length()){
                    System.out.println(1);
                }else{
                    if(s1.equals(s2)){
                        System.out.println(2);
                    }else if(s1.equalsIgnoreCase(s2)){
                        System.out.println(3);
                    }else{
                        System.out.println(4);
                    }
                }
            }
        };
        lt.run();
    }

}
  • 分解質因數
/*  
基礎練習 分解質因數  

問題描述  
  求出區間[a,b]中所有整數的質因數分解。  
輸入格式  
  輸入兩個整數a,b。  
輸出格式  
  每行輸出一個數的分解,形如k=a1*a2*a3...(a1<=a2<=a3...,k也是從小到大的)(具體可看樣例)  
樣例輸入  
3 10  
樣例輸出  
3=3  
4=2*2  
5=5  
6=2*3  
7=7  
8=2*2*2  
9=3*3  
10=2*5  
提示  
  先篩出所有素數,然後再分解。  
數據規模和約定  
  2<=a<=b<=10000  
*/   
public class Main12 {

    public static void main(String[] args) {
        LT lt=new LT("分解質因數") {
            @Override
            public void fun() {
                int a=sc.nextInt();
                int b=sc.nextInt();
                for(int i=a;i<=b;i++){
                    System.out.print(i+"=");
                    int temp=i;
                    while(temp!=1){
                        for(int j=2;j<=temp;j++){
                            if(temp%j==0){
                                temp=temp/j;
                                if(temp!=1){
                                    System.out.print(j+"*");
                                }else{
                                    System.out.print(j);
                                }
                                break;
                            }
                        }
                    } 
                    System.out.println();
                }
            }
        };
        lt.run();
    }

}
  • 矩陣乘法
    bug點:矩陣的零次冪是單位矩陣,還是需要紮實的數學基礎啊!
    關於二維數組複製可以參考:關於數組複製的方法
/*  
基礎練習 矩陣乘法  

問題描述  
  給定一個N階矩陣A,輸出A的M次冪(M是非負整數)  
  例如:  
  A =  
  1 2  
  3 4  
  A的2次冪  
  7 10  
  15 22  
輸入格式  
  第一行是一個正整數N、M(1<=N<=30, 0<=M<=5),表示矩陣A的階數和要求的冪數  
  接下來N行,每行N個絕對值不超過10的非負整數,描述矩陣A的值  
輸出格式  
  輸出共N行,每行N個整數,表示A的M次冪所對應的矩陣。相鄰的數之間用一個空格隔開  
樣例輸入  
2 2  
1 2  
3 4  
樣例輸出  
7 10  
15 22  
*/
public class Main14 {
    public static void main(String[] args) {
        LT lt = new LT("矩陣乘法") {

            @Override
            public void fun() {
                int n = sc.nextInt();
                int m = sc.nextInt();
                int[][] a = new int[n][n];
                for (int i = 0; i < n; i++) {
                    for (int j = 0; j < n; j++) {
                        a[i][j] = sc.nextInt();
                    }
                }
                int[][] t = new int[n][n];
                int[][] b = new int[n][n];
                for (int i = 0; i < a.length; i++) {
                    b[i] = a[i].clone();
                }
                if (m == 0) {
                    for (int i = 0; i < n; i++) {
                        for (int j = 0; j < n; j++) {
                            if (i == j) {
                                System.out.println(1 + " ");
                            } else {
                                System.out.println(0 + " ");
                            }
                        }
                        System.out.println();
                    }

                } else {

                    for (int k = 1; k < m; k++) {
                        for (int i = 0; i < n; i++) {
                            for (int j = 0; j < n; j++) {
                                int sum = 0;
                                for (int l = 0; l < n; l++) {
                                    sum += b[i][l] * a[l][j];
                                }
                                t[i][j] = sum;
                            }
                        }
                        for (int i = 0; i < a.length; i++) {
                            b[i] = t[i].clone();
                        }
                    }
                    for (int[] c : b) {
                        for (int d : c) {
                            System.out.print(d + " ");
                        }
                        System.out.println();
                    }
                }
            }
        };
        lt.run();
    }
}
  • 矩形面積交
/*  
基礎練習 矩形面積交 

問題描述  
  平面上有兩個矩形,它們的邊平行於直角座標系的X軸或Y軸。對於每個矩形,我們  

給出它的一對相對頂點的座標,請你編程算出兩個矩形的交的面積。  
輸入格式  
  輸入僅包含兩行,每行描述一個矩形。  
  在每行中,給出矩形的一對相對頂點的座標,每個點的座標都用兩個絕對值不超過  

10^7的實數表示。  
輸出格式  
  輸出僅包含一個實數,爲交的面積,保留到小數後兩位。  
樣例輸入  
1 1 3 3  
2 2 4 4  
樣例輸出  
1.00  
*/  
import java.util.Arrays;
public class Main13 {

    public static void main(String[] args) {
        LT lt=new LT("矩形面積交") {
            @Override
            public void fun() {
                double ax=sc.nextDouble();
                double ay=sc.nextDouble();
                double bx=sc.nextDouble();
                double by=sc.nextDouble();
                double cx=sc.nextDouble();
                double cy=sc.nextDouble();
                double dx=sc.nextDouble();
                double dy=sc.nextDouble();
                if(max(ax,bx)<min(cx,dx)||max(cx,dx)<min(ax,bx)||max(cy,dy)<min(ay,by)||max(ax,bx)<min(cx,dx)){
                    System.out.println("0.00");
                }else{
                    double []a={ax,bx,cx,dx};
                    double []b={ay,by,cy,dy};
                    Arrays.sort(a);
                    Arrays.sort(b);
                    double sum=(a[2]-a[1])*(b[2]-b[1]);
                    System.out.printf("%.2f",sum);
                }
            }
            public double max(double a,double b){
                return a>b?a:b;
            }
            public double min(double a,double b){
                return a<b?a:b;
            }
        };
        lt.run();
    }

}
  • 完美的代價
    注:本題太燒腦,我找的對應的C語言解決方案,扒完後有在代碼裏寫註釋,有問題可以問我
/*  
基礎練習 完美的代價  

問題描述  
  迴文串,是一種特殊的字符串,它從左往右讀和從右往左讀是一樣的。小龍龍認爲回  

文串纔是完美的。現在給你一個串,它不一定是迴文的,請你計算最少的交換次數使得該  

串變成一個完美的迴文串。  
  交換的定義是:交換兩個相鄰的字符  
  例如mamad  
  第一次交換 ad : mamda  
  第二次交換 md : madma  
  第三次交換 ma : madam (迴文!完美!)  
輸入格式  
  第一行是一個整數N,表示接下來的字符串的長度(N <= 8000)  
  第二行是一個字符串,長度爲N.只包含小寫字母  
輸出格式  
  如果可能,輸出最少的交換次數。  
  否則輸出Impossible  
樣例輸入  
5  
mamad  
樣例輸出  
3  
*/
public class Main15 {

    public static void main(String[] args) {
        LT lt = new LT("完美的代價") {
            @Override
            public void fun() {
                int[] b = new int[26];// 水桶
                int n = sc.nextInt();
                char x = '0';// 用來保存僅出現一次的字母
                sc.nextLine();// 在數字後會有一個回車,這裏用來把回車吐掉
                String str = sc.nextLine();
                char[] s = str.toCharArray();
                /*
                 * 水桶法,使用26個水桶檢測對應字母出現幾次,若超過一個字母出現的次數不是偶數,就不能構成迴文
                 */
                for (int i = 0; i < n; i++) {
                    int j = s[i] - 'a';
                    b[j]++;
                }
                int k = 0;// 記錄只出現奇數次字母的次數
                for (int j = 0; j < 26; j++) {
                    if (b[j] % 2 != 0) {
                        k++;
                        x = (char) (j + 'a');// 與C語言不同需要強制轉換
                    }
                }
                if (k >= 2)
                    System.out.println("Impossible");
                else
                    System.out.println(changes(s, x, n));
            }

            int changes(char s[], char x, int n) {
                int i, change = 0, j, k;
                // 最外層for循環走一遍走到中間爲止
                for (i = 0; i < n / 2; i++) {
                    // 如果是隻出現奇數次的字符
                    if (s[i] == x) {
                        /* 注意for循環界限,
                         * 首先找到與目前這個奇數次字母相對稱的字母,
                         * 從現在位置向右尋找與對對稱位置相等的字符,
                         * 然後從相等位置開始依次把前一個字母放到現在位置上一直到i處
                         * 然後i處的字母設置爲與之對稱的字母
                         */
                        for (j = i; j < n - i - 1; j++)
                            if (s[n - i - 1] == s[j])
                                break;
                        change += j - i;
                        for (k = j; k > i; k--)
                            s[k] = s[k - 1];
                        s[i] = s[n - i - 1];
                    }
                    // 其他字符
                    else {
                        /*
                         * 從該字母對稱位置向左尋找與之相等的字母
                         * 然後從相等位置開始依次把後一個字母放到現在位置上一直到對稱位置處
                         * 然後對稱位置的字母設置爲i處字母
                         */
                        for (j = n - i - 1; j >= i; j--)
                            if (s[i] == s[j])
                                break;
                        change += n - i - 1 - j;
                        for (k = j; k < n - i - 1; k++)
                            s[k] = s[k + 1];
                        s[n - i - 1] = s[i];
                    }
                }
                return change;
            }
        };
        lt.run();
    }

}
  • 數的讀法
    我的算法長,但是容易理解。
    說一下思路,
    把數字按照億級,萬級,個級,每四個數字分爲一級,可以發現這四位數字的讀法是一樣的只是讀完後要在加上億或者萬,個級不加。(print方法)
    但是根據題目要求,最開始讀的如果遇到了10億,要讀成十億,而不是一十億,但是後面的級別,如果出現了就得加1。所以又寫了一個函數專門用來解決第一次要讀的數(one方法)。
    特別要指出的是:
//C語言實現
#include<stdio.h>  
#include<string.h>  
int main()  
{  
    char a[100];int i,j,k,l;  
    char b[20][10]={"ling","yi","er","san","si","wu","liu","qi","ba","jiu"};  
    char c[20][10]={"","","shi","bai","qian","wan","shi","bai","qian","yi","shi"};  
    gets(a);  
    l=strlen(a);  
    for(i=0;i<l;i++){  
        j=a[i]-48;  
        if(j==0){if(i<l-1)if(a[i+1]!=48)printf("%s ",b[j]);}  
        else   
        if((l-i==2||l-i==6||l-i==10)&&j==1)printf("%s ",c[l-i]);  
        else if(a[i-1]==48&&j==1)printf("%s ",c[l-i]);  
        else printf("%s %s ",b[j],c[l-i]);  
    }  
    printf("\n");  
    return 0;  
}  

上面的這個方法是不滿足題目要求的“比如說“10010”讀作“yi wan ling yi shi””,用這個方法讀出來的會是一萬零十,沒有1。

/*  
基礎練習 數的讀法  

問題描述  
  Tom教授正在給研究生講授一門關於基因的課程,有一件事情讓他頗爲頭疼:一條染  

色體上有成千上萬個鹼基對,它們從0開始編號,到幾百萬,幾千萬,甚至上億。  
  比如說,在對學生講解第1234567009號位置上的鹼基時,光看着數字是很難準確的念  

出來的。  
  所以,他迫切地需要一個系統,然後當他輸入12 3456 7009時,會給出相應的念法:  
  十二億三千四百五十六萬七千零九  
  用漢語拼音表示爲  
  shi er yi san qian si bai wu shi liu wan qi qian ling jiu  
  這樣他只需要照着念就可以了。  
  你的任務是幫他設計這樣一個系統:給定一個阿拉伯數字串,你幫他按照中文讀寫的  

規範轉爲漢語拼音字串,相鄰的兩個音節用一個空格符格開。  
  注意必須嚴格按照規範,比如說“10010”讀作“yi wan ling yi shi”而不是“yi wan   

ling shi”,“100000”讀作“shi wan”而不是“yi shi wan”,“2000”讀作“er qian”而  

不是“liang qian”。  
輸入格式  
  有一個數字串,數值大小不超過2,000,000,000。  
輸出格式  
  是一個由小寫英文字母,逗號和空格組成的字符串,表示該數的英文讀法。  
樣例輸入  
1234567009  
樣例輸出  
shi er yi san qian si bai wu shi liu wan qi qian ling jiu  
*/

public class Main17 {

    public static void main(String[] args) {
        LT lt = new LT("數的讀法") {

            String[] b = { "ling", "yi", "er", "san", "si", "wu", "liu", "qi", "ba", "jiu", "shi" };

            @Override
            public void fun() {

                int num = sc.nextInt();
                if (num >= 100000000) {
                    one(num / 100000000, "yi");
                    print(num % 100000000 / 10000, "wan");
                    print(num % 10000, "");
                } else if (num >= 10000) {
                    one(num % 100000000 / 10000, "wan");
                    print(num % 10000, "");
                } else {
                    one(num % 10000, "");
                }
            }

            public void one(int n, String str) {
                String c[] = { "", "shi ", "bai ", "qian " };
                int t[] = new int[4];
                t[0] = n / 1000;
                t[1] = n % 1000 / 100;
                t[2] = n % 100 / 10;
                t[3] = n % 10;
                int i = 0;
                while (t[i++] == 0)
                    ;
                i--;
                for (; i < 4; i++) {
                    if (t[i] == 0) {
                        if (i != 3 && t[i + 1] != 0) {
                            System.out.print(b[t[i]] + " ");
                        }
                    } else if (t[i] == 1 && i == 2) {
                        System.out.print(c[4 - i - 1]);
                    } else {
                        System.out.print(b[t[i]] + " ");
                        System.out.print(c[4 - i - 1]);
                    }

                }

                System.out.print(str + " ");
            }

            public void print(int n, String str) {
                if (n == 0) {
                    return;
                }
                String c[] = { "", "shi ", "bai ", "qian " };
                int t[] = new int[4];
                t[0] = n / 1000;
                t[1] = n % 1000 / 100;
                t[2] = n % 100 / 10;
                t[3] = n % 10;
                int i = 0;

                for (; i < 4; i++) {
                    if (t[i] == 0) {
                        if (i != 3 && t[i + 1] != 0) {
                            System.out.print(b[t[i]] + " ");
                        }
                    } else {
                        System.out.print(b[t[i]] + " ");
                        System.out.print(c[4 - i - 1]);
                    }

                }

                System.out.print(str + " ");

            }
        };
        lt.run();
    }

}
  • Sine之舞
/*  
基礎練習 Sine之舞  

問題描述  
  最近FJ爲他的奶牛們開設了數學分析課,FJ知道若要學好這門課,必須有一個好的三  

角函數基本功。所以他準備和奶牛們做一個“Sine之舞”的遊戲,寓教於樂,提高奶牛們  

的計算能力。  
  不妨設  
  An=sin(1–sin(2+sin(3–sin(4+...sin(n))...)  
  Sn=(...(A1+n)A2+n-1)A3+...+2)An+1  
  FJ想讓奶牛們計算Sn的值,請你幫助FJ打印出Sn的完整表達式,以方便奶牛們做題。  
輸入格式  
  僅有一個數:N<201。  
輸出格式  
  請輸出相應的表達式Sn,以一個換行符結束。輸出中不得含有多餘的空格或換行、回  

車符。  
樣例輸入  
3  
樣例輸出  
((sin(1)+3)sin(1–sin(2))+2)sin(1–sin(2+sin(3)))+1  
*/

public class Main18 {

    public static void main(String[] args) {
        LT lt = new LT("Sine之舞") {

            @Override
            public void fun() {
                printS(sc.nextInt());

            }

            public void printA(int n) {
                for (int i = 1; i <= n; i++) {
                    System.out.print("sin(" + i);
                    if (i == n) {
                        break;
                    }
                    if (i % 2 != 0) {
                        System.out.print("-");
                    } else {
                        System.out.print("+");
                    }
                }
                for (int i = 0; i < n; i++) {
                    System.out.print(")");
                }
            }

            public void printS(int n) {
                for (int i = 1; i < n; i++) {
                    System.out.print("(");
                }
                for (int i = 1; i <= n; i++) {
                    printA(i);
                    System.out.print("+" + (n - i + 1));
                    if (i != n) {
                        System.out.print(")");
                    }
                }
            }
        };
        lt.run();
    }

}
  • FJ的字符串
    這道題真是讓我感受到了遞歸的魅力,同樣使用了PrintStream的prinf方法。
/*  
基礎練習 FJ的字符串  

問題描述  
  FJ在沙盤上寫了這樣一些字符串:  
  A1 = “A”  
  A2 = “ABA”  
  A3 = “ABACABA”  
  A4 = “ABACABADABACABA”  
  … …  
  你能找出其中的規律並寫所有的數列AN嗎?  
輸入格式  
  僅有一個數:N ≤ 26。  
輸出格式  
  請輸出相應的字符串AN,以一個換行符結束。輸出中不得含有多餘的空格或換行、  

回車符。  
樣例輸入  
3  
樣例輸出  
ABACABA  
*/  

public class Main19 {

    public static void main(String[] args) {
        LT lt = new LT("FJ的字符串") {

            @Override
            public void fun() {
                f(sc.nextInt());
            }
            public void f(int a)  
            {  
                if(a==1)
                    System.out.printf("%c",'A');  
                else   
                {
                    f(a-1);  
                    System.out.printf("%c",a-1+'A');  
                    f(a-1);  
                }
            }

        };
        lt.run();
    }

}
  • 芯片測試
    因爲正品比次品多,所以檢測數據中1多的就說明這個產品是正品。看每一列1的個數就可以了。
/*  
基礎練習 芯片測試  

問題描述  
  有n(2≤n≤20)塊芯片,有好有壞,已知好芯片比壞芯片多。  
  每個芯片都能用來測試其他芯片。用好芯片測試其他芯片時,能正確給出被測試芯片  

是好還是壞。而用壞芯片測試其他芯片時,會隨機給出好或是壞的測試結果(即此結果與  

被測試芯片實際的好壞無關)。  
  給出所有芯片的測試結果,問哪些芯片是好芯片。  
輸入格式  
  輸入數據第一行爲一個整數n,表示芯片個數。  
  第二行到第n+1行爲n*n的一張表,每行n個數據。表中的每個數據爲0或1,在這n行  

中的第i行第j列(1≤i, j≤n)的數據表示用第i塊芯片測試第j塊芯片時得到的測試結果,1  

表示好,0表示壞,i=j時一律爲1(並不表示該芯片對本身的測試結果。芯片不能對本身  

進行測試)。  
輸出格式  
  按從小到大的順序輸出所有好芯片的編號  
樣例輸入  
3  
1 0 1  
0 1 0  
1 0 1  
樣例輸出  
1 3  
*/  

public class Main20 {

    public static void main(String[] args) {
        LT lt = new LT("芯片測試") {

            @Override
            public void fun() {
                int n=sc.nextInt();
                int [][]a=new int[n][n];
                for(int i=0;i<n;i++){
                    for(int j=0;j<n;j++){
                        a[i][j]=sc.nextInt();
                    }
                }
                for(int i=0;i<n;i++){
                    int sum=0;
                    for(int j=0;j<n;j++){
                        sum+=a[j][i];
                    }
                    if(2*sum>n){
                        System.out.print(i+1+" ");
                    }
                }
            }
        };
        lt.run();
    }
}
  • 龜兔賽跑預測
/*  
基礎練習 龜兔賽跑預測  

問題描述  
  話說這個世界上有各種各樣的兔子和烏龜,但是研究發現,所有的兔子和烏龜都有一個共同的特點——喜歡賽跑。於是世界上各個角落都不斷在發生着烏龜
和兔子的比賽,小華對此很感興趣,於是決定研究不同兔子和烏龜的賽跑。他發現,兔子雖然跑比烏龜快,但它們有衆所周知的毛病——驕傲且懶惰,於是在與
烏龜的比賽中,一旦任一秒結束後兔子發現自己領先t米或以上,它們就會停下來休息s秒。對於不同的兔子,t,s的數值是不同的,但是所有的烏龜卻是一致——它們不到終點決不停止。  
  然而有些比賽相當漫長,全程觀看會耗費大量時間,而小華髮現只要在每場比賽開始後記錄下兔子和烏龜的數據——兔子的速度v1(表示每秒兔子能跑v1米),烏龜的速度v2,以及兔子對應的t,s值,以及賽道的長度l——就能預測出比賽的結果。但是小華很懶,不想通過手工計算推測出比賽的結果,於是他找到了你——清華大學計算機系的高才生——請求幫助,請你寫一個程序,對於輸入的一場比賽的數據v1,v2,t,s,l,預測該場比賽的結果。  
輸入格式  
  輸入只有一行,包含用空格隔開的五個正整數v1,v2,t,s,l,其中(v1,v2<=100;t<=300;s<=10;l<=10000且爲v1,v2的公倍數)  
輸出格式  
  輸出包含兩行,第一行輸出比賽結果——一個大寫字母“T”或“R”或“D”,分別表示烏龜獲勝,兔子獲勝,或者兩者同時到達終點。  
  第二行輸出一個正整數,表示獲勝者(或者雙方同時)到達終點所耗費的時間(秒數)。  
樣例輸入  
10 5 5 2 20  
樣例輸出  
D  
4  
樣例輸入  
10 5 5 1 20  
樣例輸出  
R  
3  
樣例輸入  
10 5 5 3 20  
樣例輸出  
T  
4   
*/  

public class Main21 {

    public static void main(String[] args) {
        LT lt = new LT("龜兔賽跑預測") {

            @Override
            public void fun() {
                int v1=sc.nextInt();
                int v2=sc.nextInt();
                int t=sc.nextInt();
                int s=sc.nextInt();
                int l=sc.nextInt();
                int l1=0,l2=0;
                int i=0;
                while(true){
                    i++;
                    l1+=v1;
                    l2+=v2;
                    if(l1==l||l2==l){
                        break;
                    }
                    if(l1-l2>=t){
                        l1-=s*v1;
                    }
                }
                System.out.println((l1==l?l2==l?"D":"R":"T")+"\n"+i);
            }
        };
        lt.run();
    }
}
  • 回形取數
/*  
基礎練習 回形取數    

問題描述  
  回形取數就是沿矩陣的邊取數,若當前方向上無數可取或已經取過,則左轉90度。一開始位於矩陣左上角,方向向下。  
輸入格式  
  輸入第一行是兩個不超過200的正整數m, n,表示矩陣的行和列。接下來m行每行n個整數,表示這個矩陣。  
輸出格式  
  輸出只有一行,共mn個數,爲輸入矩陣回形取數得到的結果。數之間用一個空格分隔,行末不要有多餘的空格。  
樣例輸入  
3 3  
1 2 3  
4 5 6  
7 8 9  
樣例輸出  
1 4 7 8 9 6 3 2 5  
樣例輸入  
3 2  
1 2  
3 4  
5 6  
樣例輸出  
1 3 5 6 4 2  
*/  

public class Main22 {

    public static void main(String[] args) {
        LT lt = new LT("回形取數") {

            @Override
            public void fun() {
                int m=sc.nextInt();
                int n=sc.nextInt();
                int [][]a=new int [m][n];
                for(int i=0;i<m;i++){
                    for(int j=0;j<n;j++){
                        a[i][j]=sc.nextInt();
                    }
                }
                int times=0;
                int x=0,y=0;
                while(times!=n*m){
                    for(;x<m&&a[x][y]!=-1;x++){
                        System.out.print(a[x][y]+" ");
                        a[x][y]=-1;
                        times++;
                    }
                    x--;
                    y++;
                    for(;y<n&&a[x][y]!=-1;y++){
                        System.out.print(a[x][y]+" ");
                        a[x][y]=-1;
                        times++;
                    }
                    y--;
                    x--;
                    for(;x>=0&&a[x][y]!=-1;x--){
                        System.out.print(a[x][y]+" ");
                        a[x][y]=-1;
                        times++;
                    }
                    x++;
                    y--;
                    for(;y>=0&&a[x][y]!=-1;y--){
                        System.out.print(a[x][y]+" ");
                        a[x][y]=-1;
                        times++;
                    }
                    y++;
                    x++;
                }
            }
        };
        lt.run();
    }
}
  • 報時助手
/*  
基礎練習 報時助手  

問題描述  
  給定當前的時間,請用英文的讀法將它讀出來。  
  時間用時h和分m表示,在英文的讀法中,讀一個時間的方法是:  
  如果m爲0,則將時讀出來,然後加上“o'clock”,如3:00讀作“three o'clock”。  
  如果m不爲0,則將時讀出來,然後將分讀出來,如5:30讀作“five thirty”。  
  時和分的讀法使用的是英文數字的讀法,其中0~20讀作:  
  0:zero, 1: one, 2:two, 3:three, 4:four, 5:five, 6:six, 7:seven, 8:eight, 9:nine, 10:ten, 11:eleven, 12:twelve, 13:thirteen, 14:fourteen, 15:fifteen, 16:sixteen, 17:seventeen, 18:eighteen, 19:nineteen, 20:twenty。  
  30讀作thirty,40讀作forty,50讀作fifty。  
  對於大於20小於60的數字,首先讀整十的數,然後再加上個位數。如31首先讀30再加1的讀法,讀作“thirty one”。  
  按上面的規則21:54讀作“twenty one fifty four”,9:07讀作“nine seven”,0:15讀作“zero fifteen”。  
輸入格式  
  輸入包含兩個非負整數h和m,表示時間的時和分。非零的數字前沒有前導0。h小於24,m小於60。  
輸出格式  
  輸出時間時刻的英文。  
樣例輸入  
0 15  
樣例輸出  
zero fifteen  
*/  

public class Main23 {

    public static void main(String[] args) {
        LT lt = new LT("報時助手") {
            String [] a={"zero" ,"one" ,"two" ,"three" ,"four" ,"five" ,"six" ,"seven" ,"eight" ,"nine" ,"ten" ,
                    "eleven" ,"twelve" ,"thirteen" ,"fourteen" ,"fifteen" ,"sixteen" ,"seventeen" ,"eighteen" ,"nineteen" ,"twenty"};
            String [] b={"twenty","thirty","forty","fifty"};
            @Override
            public void fun() {
                int h=sc.nextInt();
                int m=sc.nextInt();
                change(h);
                if(m==0){
                    System.out.println(" o'clock");
                }else{
                    change(m);
                }
            }
            public void change(int s){
                if(s<=20){
                    System.out.print(a[s]);
                }else if(s%10==0){
                    System.out.print(b[s/10-2]);
                }else{
                    System.out.print(b[s/10-2]+" "+a[s%10]);
                }
            }
        };
        lt.run();
    }
}
  • 2n皇后問題
    黑白皇后可以當做兩個皇后問題,先方白皇后再放黑皇后。
/*  
基礎練習 2n皇后問題  

問題描述  
  給定一個n*n的棋盤,棋盤中有一些位置不能放皇后。現在要向棋盤中放入n個黑皇后  

和n個白皇后,使任意的兩個黑皇后都不在同一行、同一列或同一條對角線上,任意的兩  

個白皇后都不在同一行、同一列或同一條對角線上。問總共有多少种放法?n小於等於8。  
輸入格式  
  輸入的第一行爲一個整數n,表示棋盤的大小。  
  接下來n行,每行n個0或1的整數,如果一個整數爲1,表示對應的位置可以放皇后,  

如果一個整數爲0,表示對應的位置不可以放皇后。  
輸出格式  
  輸出一個整數,表示總共有多少种放法。  
樣例輸入  
4  
1 1 1 1  
1 1 1 1  
1 1 1 1  
1 1 1 1  
樣例輸出  
2  
樣例輸入  
4  
1 0 1 1  
1 1 1 1  
1 1 1 1  
1 1 1 1  
樣例輸出  
0  
*/

public class Main24 {

    public static void main(String[] args) {
        LT lt = new LT("2n皇后問題") {
            int bqueen[];// 黑皇后
            int wqueen[];// 白皇后
            int chessboard[][];// 1:能放 0:不能放
            int count;
            int n;

            public void BlackQueen(int k){
                int i;
                int j;
                for (i = 0; i < k - 1; i++) {
                    int judge = bqueen[i] - bqueen[k - 1];
                    if (judge == 0 || Math.abs(k - 1 - i) == Math.abs(judge))
                        return;
                }
                if (k == n) {
                    count++;
                    return;
                }
                for (j = 0; j < n; j++) {
                    if (j != wqueen[k] && chessboard[k][j] == 1) {
                        bqueen[k] = j;
                        BlackQueen(k + 1);
                    }
                }
            }

            void WhiteQueen(int k) {
                int i;
                int j;
                for (i = 0; i < k - 1; i++) {
                    int judge = wqueen[i] - wqueen[k - 1];
                    if (judge == 0 || Math.abs(k - 1 - i) == Math.abs(judge))
                        return;
                }
                if (k == n) {
                    BlackQueen(0);
                    return;
                }
                for (j = 0; j < n; j++) {
                    if (chessboard[k][j] == 1) {
                        wqueen[k] = j;
                        WhiteQueen(k + 1);
                    }
                }
            }

            public void fun() {
                int i;
                int j;
                count=0;
                n = sc.nextInt();
                chessboard = new int[n][n];
                bqueen =new int[n];
                wqueen=new int[n];
                for (i = 0; i < n; i++)
                    for (j = 0; j < n; j++)
                        chessboard[i][j] = sc.nextInt();
                WhiteQueen(0);
                System.out.print(count);
            }
        };
        lt.run();
    }
}
  • Huffuman樹
/*  
基礎練習 Huffuman樹   

問題描述  
  Huffman樹在編碼中有着廣泛的應用。在這裏,我們只關心Huffman樹的構造過程。  
  給出一列數{pi}={p0, p1, …, pn-1},用這列數構造Huffman樹的過程如下:  
  1. 找到{pi}中最小的兩個數,設爲pa和pb,將pa和pb從{pi}中刪除掉,然後將它們的  

和加入到{pi}中。這個過程的費用記爲pa + pb。  
  2. 重複步驟1,直到{pi}中只剩下一個數。  
  在上面的操作過程中,把所有的費用相加,就得到了構造Huffman樹的總費用。  
  本題任務:對於給定的一個數列,現在請你求出用該數列構造Huffman樹的總費用。  

  例如,對於數列{pi}={5, 3, 8, 2, 9},Huffman樹的構造過程如下:  
  1. 找到{5, 3, 8, 2, 9}中最小的兩個數,分別是2和3,從{pi}中刪除它們並將和5加入,  

得到{5, 8, 9, 5},費用爲5。  
  2. 找到{5, 8, 9, 5}中最小的兩個數,分別是5和5,從{pi}中刪除它們並將和10加入,得  

到{8, 9, 10},費用爲10。  
  3. 找到{8, 9, 10}中最小的兩個數,分別是8和9,從{pi}中刪除它們並將和17加入,得  

到{10, 17},費用爲17。  
  4. 找到{10, 17}中最小的兩個數,分別是10和17,從{pi}中刪除它們並將和27加入,得  

到{27},費用爲27。  
  5. 現在,數列中只剩下一個數27,構造過程結束,總費用爲5+10+17+27=59。  
輸入格式  
  輸入的第一行包含一個正整數n(n<=100)。  
  接下來是n個正整數,表示p0, p1, …, pn-1,每個數不超過1000。  
輸出格式  
  輸出用這些數構造Huffman樹的總費用。  
樣例輸入  
5 3 8 2 9  
樣例輸出  
*/

public class Main25 {

    public static void main(String[] args) {
        LT lt = new LT("Huffuman樹") {
            int a[] = new int[100];

            public void fun() {
                int n =  sc.nextInt();

                for(int i=0;i<n;i++) {
                    a[i] = sc.nextInt();

                }

                int sum = 0;
                for (int i = 0; i < n - 1; i++) {
                    sort(i, n);
                    a[i+1]+=a[i];
                    sum+=a[i+1];
//                  System.out.println(sum);
                }
                System.out.println(sum);
            }

            public void sort(int start, int n) {
                for (int i = start; i < n; i++) {
                    for (int j = i+1; j < n ; j++) {
                        if (a[j] < a[i]) {
                            int temp = a[j];
                            a[j] = a[i];
                            a[i] = temp;
                        }
                    }
                }
            }
        };
        lt.run();
    }
}
  • 高精度加法
    我是怕最高位想加會大於10特意在n數組多留了一個空,其實是多餘的。
/*  
基礎練習 高精度加法  

問題描述  
  輸入兩個整數a和b,輸出這兩個整數的和。a和b都不超過100位。  
算法描述  
  由於a和b都比較大,所以不能直接使用語言中的標準數據類型來存儲。對於這種問題  

,一般使用數組來處理。  
  定義一個數組A,A[0]用於存儲a的個位,A[1]用於存儲a的十位,依此類推。同樣可以  

用一個數組B來存儲b。  
  計算c = a + b的時候,首先將A[0]與B[0]相加,如果有進位產生,則把進位(即和的  

十位數)存入r,把和的個位數存入C[0],即C[0]等於(A[0]+B[0])%10。然後計算A[1]與B[1]  

相加,這時還應將低位進上來的值r也加起來,即C[1]應該是A[1]、B[1]和r三個數的和.如  

果又有進位產生,則仍可將新的進位存入到r中,和的個位存到C[1]中。依此類推,即可求  

出C的所有位。  
  最後將C輸出即可。  
輸入格式  
  輸入包括兩行,第一行爲一個非負整數a,第二行爲一個非負整數b。兩個整數都不超  

過100位,兩數的最高位都不是0。  
輸出格式  
  輸出一行,表示a + b的值。  
樣例輸入  
20100122201001221234567890  
2010012220100122  
樣例輸出  
20100122203011233454668012  
*/  

public class Main26 {

    public static void main(String[] args) {
        LT lt = new LT("高精度加法") {


            public void fun() {
                String src=sc.nextLine();
                char[]a=src.toCharArray();
                src=sc.nextLine();
                char[]b=src.toCharArray();
                if(a.length>b.length){
                    caculate(a,b);
                }else{
                    caculate(b,a);
                }
            }
            public void caculate(char [] l,char []s){

                int ll=l.length;
                int sl=s.length;
                int n[]=new int [ll+1];
                int i;
                for(i = 0;i<sl;i++){
                    n[ll-i-1]=(l[ll-i-1]+s[sl-i-1]-48-48+n[ll-i])/10;
                    n[ll-i]=(l[ll-i-1]+s[sl-i-1]-48-48+n[ll-i])%10;
                }
                for(;i<ll;i++){
                    n[ll-i-1]=(l[ll-i-1]-48+n[ll-i])/10;
                    n[ll-i]=(l[ll-i-1]-48+n[ll-i])%10;
                }
                i=0;
                while(n[i++]==0);
                for(i=i-1;i<ll+1;i++){
                    System.out.print(n[i]);
                }
            }
        };
        lt.run();
    }
}
  • 階乘計算
    2600個長度是滿足的,不然會不夠用
/*  
基礎練習 階乘計算  

問題描述  
  輸入一個正整數n,輸出n!的值。  
  其中n!=1*2*3*…*n。  
算法描述  
  n!可能很大,而計算機能表示的整數範圍有限,需要使用高精度計算的方法。使用一個數組A來表示一個大整數a,A[0]表示a的個位,A[1]表示a的十位,依次類推。  
  將a乘以一個整數k變爲將數組A的每一個元素都乘以k,請注意處理相應的進位。  
  首先將a設爲1,然後乘2,乘3,當乘到n時,即得到了n!的值。  
輸入格式  
  輸入包含一個正整數n,n<=1000。  
輸出格式  
  輸出n!的準確值。  
樣例輸入  
10  
樣例輸出  
3628800  
*/  

public class Main27 {

    public static void main(String[] args) {
        LT lt = new LT("階乘計算") {


            public void fun() {
                int a[]=new int[2600];
                int n=sc.nextInt();
                a[0] = 1;  
                for(int i=2;i<=n;i++){
                    int extra=0;
                    for(int j=0;j<a.length;j++){
                        int temp=a[j]*i+extra;
                        extra=temp/10;
                        a[j]=temp%10;
                    }
                }
                int i=a.length;
               while(a[--i]==0);
              for(;i>=0;i--){
                  System.out.print(a[i]);
              }
            }



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