Java練習十題集(一)

Java練習十題集(一):java

1. 將26個字母按形如手機鍵盤的對應形式轉換成0~9數字。git

2.寫兩個線程,其中一個線程打印1-52,另外一個打印A-Z,打印順序爲12A34B56C....5152Z。數組

3.使用循壞輸出九九乘法表。輸出以下結果:ide

1×1=1
2×1=2,2×2=4
3×1=3,3×2=6,3×3=9
......
9×1=9,9×2=18,9×3=27,… 9×9=81函數

4.Java求最大公約數和最小公倍數。this

5.2~200之間有多少素數,分別是多少。spa

6.分別編寫出計算Hermite多項式Hn (x) 值的遞推和遞歸函數。Hn (x) 定義爲
H0(x)=1 (n=0)
H1(x)=2x (n=1)
Hn(x)=2xHn-1(x) -2(n- 1)Hn-2(x) (n>1)線程

7.寫出計算Ackemam函數Ack (m, n)的遞歸計算函數。對於m≥0, n≥0, Ack(m,n)定義爲:
Ack(0,n)= n+1
Ack(m,0)= Ack(m-1,1)
Ack(m,n)= Ack(m- 1,Ack(m,n-1)) 對象

8.編寫函數,求1-3+5-7+...+n。提示:函數應該有一個參數,經過這個參數獲得n的值。blog

9.小萌的副本生涯

【題目描述】
在主城站街好久以後,小萌決定不能就這樣的浪費時間虛度青春,他打算去打副本。
此次的副本只有一個BOSS,並且BOSS是不須要擊殺的,只須要和它比智力…….
BOSS會列出一正整數的序列,由小萌先開始,而後兩我的輪流從序列的任意一端取數,取得的數
累加到積分裏,當全部數都取完,遊戲結束。
假設小萌和BOSS都很聰明,兩我的取數的方法都是最優策略,問最後兩人得分各是多少。
輸入
第一行:一個正整數N(2 ≤ N ≤ 100),表示序列中正整數的個數。
第二行至末尾:用空格隔開的N個正整數(1 ≤ a[i] ≤ 200)
輸出
只有一行,用空格隔開的兩個數,小萌的得分和BOSS的得分。
樣例輸入
6
4 7 2 9 5 2
樣例輸出
11 18

10.假設你能重返過去,如今讓你回到2015年,你能選擇一支股票進行投資,你擁有這支股票將來n天的價格走勢圖,爲了躲避證監會
的監控,你只有一次買入賣出機會。如今要求實現一個程序計算哪天買入哪天賣出能得到最大收益。

輸入
第一行爲天數n
接下來n行 爲數組的n個整數元素,表明第n天該股票的價格
輸出
輸出爲b,s #表明第b天買入,第s天賣出
天數從0開始
若是沒有適合的買入賣出輸出-1,-1
一樣收益時越晚買入越早賣出更符合須要
樣例輸入
5
2
1
4
5
3
樣例輸出
1, 3

1. 將26個字母按形如手機鍵盤的對應形式轉換成0~9數字。

import java.util.HashMap;
import java.util.Map;

public class Test {

    public static void main(String[] args) {
        Map<String,Integer> map = getMapKV();
        String str = "Hello World";
        System.out.println(str);
        str = str.toUpperCase();
        for(int i = 0; i < str.length(); i++) {
            System.out.print(map.get(str.charAt(i)+""));
        }
        System.out.println( );
    }

    static Map<String,Integer> getMapKV(){
        Map<String,Integer> map = new HashMap<>();
        map.put(" ", 0);
        for(int i = 0; i < 26; i++){
            String key = "" + (char)(i+'A');
            if(i<3){
                map.put(key,2);
            }else if(i < 6){
                map.put(key,3);
            }else if(i < 9){
                map.put(key,4);
            }else if(i < 12){
                map.put(key,5);
            }else if(i < 15){
                map.put(key,6);
            }else if(i < 18){
                map.put(key,7);
            }else if(i < 21){
                map.put(key,8);
            }else if(i < 26){
                map.put(key,9);
            }
        }
        return map;
    }

}

 

2.寫兩個線程,其中一個線程打印1-52,另外一個打印A-Z,打印順序爲12A34B56C....5152Z。

public class Test {

    public static void main(String[] args) {
        threadTest();
    }
    static void threadTest(){
        Object obj = new Object();
        Digit digit = new Digit(obj);
        Letter letter = new Letter(obj);
        Thread th1 = new Thread(digit);
        Thread th2 = new Thread(letter);
        th1.start();//數字的線程先運行,數字先執行
        th2.start();
    }
}
class Digit implements Runnable{
    private Object obj;//聲明一個類的引用
    public Digit(Object obj){
        this.obj = obj;	//經過構造器將共享的資源-->對象傳進來
    }
    @Override
    public void run() {
        synchronized(obj){//給共享資源上鎖
            for(int i = 1;i < 53;i++ ){
                System.out.print(i);
                if(i % 2 == 0){
                    obj.notify();//喚醒其餘線程
                    try {
                        obj.wait();//等待並釋放鎖
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

class Letter implements Runnable{    
    private Object obj;
    public Letter(Object obj){
        this.obj = obj;
    }
    @Override
    public void run() {
        synchronized(obj){
            for(int i = 0;i < 26;i++ ){
                System.out.print((char)(i+'A'));
                obj.notify();//喚醒其餘線程
                try {
                    obj.wait();//釋放鎖等待
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

3.使用循壞輸出九九乘法表。輸出以下結果:

1×1=1
2×1=2,2×2=4
3×1=3,3×2=6,3×3=9
......
9×1=9,9×2=18,9×3=27,… 9×9=81

public class Test {

    public static void main(String[] args) {
        multiplicationTableTest();
        System.out.println("----------------------");
        multiplicationTableRecursionTest(9);
    }

    static void multiplicationTableTest() {
        for (int i = 1; i <= 9; i++) {
            for (int j = 1; j <= i; j++) {
                if (i == j) {
                    System.out.print(j + "*" + i + "=" + i * j + " ");
                } else {
                    System.out.print(j + "*" + i + "=" + i * j + ",");
                }
            }
            System.out.println();
        }
    }

    /**
     * 使用遞歸方法輸出99乘法表
     */
    public static void multiplicationTableRecursionTest(int i) {
        if (i == 1) {
            System.out.println("1*1=1");
        } else {
            multiplicationTableRecursionTest(i - 1);
            for (int j = 1; j <= i; j++) {
                if (i == j) {
                    System.out.print(j + "*" + i + "=" + i * j + " ");
                } else {
                    System.out.print(j + "*" + i + "=" + i * j + ",");
                }
            }
            System.out.println(" ");
        }
    }
}

  

4.Java求最大公約數和最小公倍數。

 

public class Test {

    public static void main(String[] args) {

        int gcd = greatestCommonDivisor(120,90);
        System.out.println(gcd);
        int lcm = leastCommonMultiple(120,90);
        System.out.println(lcm);

    }

    static int greatestCommonDivisor(int m, int n){
        //展轉相除
        int result = 0;
        int max = m > n ? m : n;
        int min = m > n ? n : m;
        if(max % min == 0){
            result = min;
        }
        while(max % min > 0){
            max = max % min;
            if(max < min) {  //交換再展轉相除
                max = max + min;
                min = max - min;
                max = max - min;
            }
            if(max % min == 0){
                result =  min;
            }
        }
        return result;
    }

    static int leastCommonMultiple(int m, int n) {
        int ret = m * n / greatestCommonDivisor(m, n);
        return ret;
    }

}

  

5.2~200之間有多少素數,分別是多少。

素數又稱質數,有無限個。質數(素數)定義爲在大於1的天然數中,除了1和它自己之外再也不有其餘因數。

public class Test {

    public static void main(String[] args) {

        primeTest();

    }

    static void primeTest(){
        System.out.println("1~200內的素數有:");
        int count = 0;
        for(int i = 1; i < 200; i++){
            if(isPrime(i)){
                System.out.print(i+",");
                count++;
            }
        }
        System.out.println();
        System.out.println("共有"+count+"個素數。");
    }

    static boolean isPrime(int n ){

        boolean bln = true;
        if(n < 2 ){
            return false;
        }
        for(int i = 2; i < n/2; i++) {
            if(n%i == 0 ){
                bln = false;
                return bln;
            }
        }
        return bln;
    }

}

  

6.分別編寫出計算Hermite多項式Hn (x) 值的遞推和遞歸函數。Hn (x) 定義爲
H0(x)=1 (n=0)
H1(x)=2x (n=1)
Hn(x)=2xHn-1(x) -2(n- 1)Hn-2(x) (n>1)

public class Test {

    public static void main(String[] args) {
        testRecH();
    }

    static void testRecH(){
        int x = recH(12);
        System.out.println(x);
    }

    static int recH(int n){
        if(n == 0){
            return 1;
        }else if(n == 1){
            return 2*n;
        }else {
            return 2*recH(n-1)-2*(n-1)*recH(n-2);
        }
    }
}

  

7.寫出計算Ackemam函數Ack (m, n)的遞歸計算函數。對於m≥0, n≥0, Ack(m,n)定義爲:
Ack(0,n)= n+1
Ack(m,0)= Ack(m-1,1)
Ack(m,n)= Ack(m- 1,Ack(m,n-1)) 

public class TestAck {

    public static void main(String[] args) {
        testRrecAck();
    }

    static void testRrecAck(){
        long x = recAck(3,5);
        System.out.println(x);
    }

    static long recAck(long m, long n){
        if(m == 0){
            return n+1;
        } if(n == 0){
            return recAck(m-1,1);
        }else{
            return recAck(m-1,recAck(m,n-1));
        }
    }
}

  

8.編寫函數,求1-3+5-7+...+n。提示:函數應該有一個參數,經過這個參數獲得n的值。

 

public class Test {

    public static void main(String[] args) {
        testSumFunc(12);
    }

    static void testSumFunc(int n) {
        for(int i = 1; i <= n; i++) {
            if(i%2 == 0) {
                int x = (-1)*(2*i-1);
                System.out.print(x);
            }else {
                if(i != 1) {
                    System.out.print("+"+(2*i-1));
                } else {
                    System.out.print(i);
                }
            }
        }
        System.out.print("=" + sumFunc(n));
        System.out.println( );
    }

    static int sumFunc(int m) {
        int sum = 0;
        for(int i = 1; i <= m; i++) {
            if(i%2 == 0) {
                sum += (-1)*(2*i-1);
            }else {
                sum+= (2*i-1);
            }
        }
        return sum;
    }

    static int sumFunc2(int m) {
       if (m % 2 == 0) {
            return -m;
       } else {
           return m;
       }
    }
}

  

9. 小萌的副本生涯

【題目描述】
在主城站街好久以後,小萌決定不能就這樣的浪費時間虛度青春,他打算去打副本。
此次的副本只有一個BOSS,並且BOSS是不須要擊殺的,只須要和它比智力…….
BOSS會列出一正整數的序列,由小萌先開始,而後兩我的輪流從序列的任意一端取數,取得的數
累加到積分裏,當全部數都取完,遊戲結束。
假設小萌和BOSS都很聰明,兩我的取數的方法都是最優策略,問最後兩人得分各是多少。
輸入
第一行:一個正整數N(2 ≤ N ≤ 100),表示序列中正整數的個數。
第二行至末尾:用空格隔開的N個正整數(1 ≤ a[i] ≤ 200)
輸出
只有一行,用空格隔開的兩個數,小萌的得分和BOSS的得分。
樣例輸入
6
4 7 2 9 5 2
樣例輸出
11 18

 

import java.util.Arrays;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Test();
    }

    static void Test() {
        System.out.print("第一行:一個正整數N(2 ≤ N ≤ 100),表示序列中正整數的個數。");
        Scanner in = new Scanner(System.in);
        int x = in.nextInt();
        System.out.println(x);
        in = new Scanner(System.in);
        String s = in.nextLine();
        System.out.println(s);
        String[] str = s.split("\\s+");
        System.out.println("數組長度:"+str.length);
        int[] arr = new int[str.length];
        for (int i = 0; i < arr.length; i++) {
            if(!"".equals(str[i].trim())){
                arr[i] = Integer.parseInt(str[i].trim());
            }
        }
        System.out.println(Arrays.toString(arr));
        play(arr);
    }

    static void play(int[] arr){
        int cute = 0, boss = 0;
        int i = 0, k = arr.length-1;
        int count = 0;
        while (i <= k){
            if(arr[i] >= arr[k]){
                if(count % 2 == 0){
                    cute += arr[i];
                }else{
                    boss += arr[i];
                }
                i++;
            } else {
                if(count % 2 == 0){
                    cute += arr[k];
                }else{
                    boss += arr[k];
                }
                k--;
            }
            count++;
        }
        System.out.println(cute+" "+boss);
    }

}

  

 

10.假設你能重返過去,如今讓你回到2015年,你能選擇一支股票進行投資,你擁有這支股票將來n天的價格走勢圖,爲了躲避證監會
的監控,你只有一次買入賣出機會。如今要求實現一個程序計算哪天買入哪天賣出能得到最大收益。

輸入
第一行爲天數n
接下來n行 爲數組的n個整數元素,表明第n天該股票的價格
輸出
輸出爲b,s #表明第b天買入,第s天賣出
天數從0開始
若是沒有適合的買入賣出輸出-1,-1
一樣收益時越晚買入越早賣出更符合須要
樣例輸入
5
2
1
4
5
3
樣例輸出
1, 3

import java.util.Arrays;
import java.util.Scanner;

public class Test {

    public static void main(String[] args) {
        Test();
    }

    static void Test(){
        System.out.print("please enter N : ");
        Scanner in  = new Scanner(System.in);
        System.out.println();
        int n = in.nextInt();
        int[] arr = new int[n];
        for(int i = 0; i < n; i++){
            in  = new Scanner(System.in);
            arr[i] =  in.nextInt();
        }
//        System.out.println(Arrays.toString(arr));
        int max = indexExtremeValueArr(arr,arr.length,true);
        int min = indexExtremeValueArr(arr,max,false);
        if(max == 0 && min == 0){
            System.out.println("-1, -1");
        }else {
            System.out.println(min+" , "+max);
        }

    }

    /**
     * 0~ x 中最大或者最小數的位置
     * @param arr
     * @param x
     * @param flag true取最大值,false取最小值(數組前x個數中)
     * @return
     */
    static int indexExtremeValueArr(int[] arr,int x, boolean flag){
        int index = 0;
        x = x < arr.length ? x : arr.length;
        if(arr.length == 1) {
            return 0;
        }
        if(flag){   //或取極大值的位置
            for(int i = 1; i < x; i++) {
                if(arr[i] > arr[i-1]){
                    index = i;
                }
            }
        } else {    //或取極小值的位置
            for(int i = 1; i < x; i++) {
                if(arr[i] <= arr[i-1]){
                    index = i;
                }
            }
        }
        return index;
    }
}
相關文章
相關標籤/搜索