Java編程題(8)

這是我第8次給大家分享題,如果想看看以前的例題,請點開我的博客,查看作業分欄。謝謝收看。

Demo05_03

在這裏插入圖片描述
這個題說難也不難就分爲兩部
1.獲取用戶的輸入 只不過第一個輸入的數據時數據的個數(數組的長度)
2.對數組進行有序的判斷
只不過第二步判斷需要一點思想:

import java.util.*;
class Demo05_03{
    public static void main(String[] args){
        //1.獲取用戶的輸入 只不過第一個輸入的數據時數據的個數(數組的長度)
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter a list:");
        int len=scanner.nextInt();//獲取的第一個數值就是數組的長度
        int[] arr=new int[len];
        for(int i=0;i<arr.length;i++){
            arr[i]=scanner.nextInt();
        }
        //2.對數組進行有序的判斷
        if(isSorted(arr)){
            System.out.println("The list is already sorted.");
        }else{
            System.out.println("The list is not sorted.");
        }
    }
    public static boolean isSorted(int[] list){
        //如果不是升序排列 那麼勢必會出現有一組數據 左大右小的情況
        for(int i=1;i<list.length;i++){
            if(list[i-1]>list[i]){
                return false;
            }
        }
        return true;
    }

}

Demo05_04

在這裏插入圖片描述
豆機也是我們生活中挺常見的一個東西,比較類似的就是那個投幣機,投幣機就是差不多就是這個結構
我們先來分析下

輸入的數據:槽子的個數 球的個數=路徑的個數
           創建槽子的具體的容器int[]
           每一個小球下落的路徑L R 字符串
           對於每一個小球而言其路徑中的步驟是隨機產生L R
1.提示用戶輸入槽子的個數和小球的個數
2.根據已有的槽子的個數去創建槽子容器
3.根據已有的球和槽子的個數去隨機創建一個小球下落的路徑
4.路徑中經過幾個釘子?路徑的步驟有幾步 和槽子的個數有關
5.如何通過路徑的經過得知最終所落入的槽子?

這就是我們的問題,那具體要這麼解決呢?
1.輸入數據
2.建立數組
3.幾個球幾個路徑path
4.根據槽子的個數計算每一個球下落的路徑
5.判斷入槽位置,只要看當前路徑中R的個數即可
6.輸出
下面是代碼:

import java.util.*;
class Demo05_04{
   
    public static void main(String[] args){
        //1.
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter the number of balls to drop:");
        int balls=scanner.nextInt();
        System.out.print("Enter the number of slots in the bean machine:");
        int slots=scanner.nextInt();
        //2.
        int[] arr=new int[slots];
        //3.幾個球幾個路徑path
        for(int i=0;i<balls;i++){
            String path=getPath(slots);
            System.out.println(path);
            //5.只要看當前路徑中R的個數即可
            arr[getR(path)]++;
        }
        //6.輸出
        System.out.println(Arrays.toString(arr));
        show(arr);
    }
    public static void show(int[] arr){
        int w=arr.length;
        int h=0;
        for(int i=0;i<arr.length;i++){
            if(arr[i]>h){
                h=arr[i];
            }
        }
        for(int i=h-1;i>=0;i--){
            for(int j=0;j<w;j++){
                if(i<arr[j]){
                    System.out.print("O");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }

    }
    public static int getR(String path){
        int count=0;
        for(int i=0;i<path.length();i++){
            if(path.charAt(i)=='R'){
                count++;
            }
        }
        return count;
    }
    public static String getPath(int slots){
        //4.根據槽子的個數計算每一個球下落的路徑
        Random random=new Random();
        String path="";
        for(int j=0;j<slots-1;j++){
            if(random.nextInt(2)==0){   //向左
                path+="L";
            }else{  //向右
                path+="R";
            }
        }
        return path;
    }
}

Demo05_05

在這裏插入圖片描述
看這個題之前我們要明白,真麼能看出兩列表相同呢?
判斷兩個數組是否完全相同
1.先判斷長度
2.再依次判斷元素大小
這就解決了

class Demo05_05{
    public static void main(String[] args){
        int[] list1={1,2,3,4,5,6,7};
        int[] list2={1,2,3,4,5,7,6};
        System.out.println(equals(list1,list2));
    }
    public static boolean equals(int[] list1,int[] list2){
        //1
        if(list1.length!=list2.length){
            return false;
        }
        //2
        for(int i=0;i<list1.length;i++){
            if(list1[i]!=list2[i]){
                return false;
            }
        }
        return true;
    }
}

Demo05_06

在這裏插入圖片描述
判斷是否有四個連續的數,就直接定義2指針,一個指針指向第一個位置,另一個指向下一個位置,如果相等count++,當count=4,結束循環,不相等,第一個指針指向第二個指針當前位置,在進行判斷,知道結束,沒有就不輸出

class Demo05_06{
    public static void main(String[] args){
        int[] arr={1,1,1,1,2,2,2,2,2,3,3,3,3,3,4};
        for(int i=0;i<arr.length;){
            int count=1;
            for(int j=i+1;j<arr.length;j++){
                if(arr[i]==arr[j]){
                    count++;
                }else{
                    break;
                }
            }
            if(count>=4){
                System.out.println(arr[i]);
                return;
            }
            i+=count;
        }
        System.out.println("沒有!");
    }
}

Demo05_07

在這裏插入圖片描述

有序數組的合併
最主要的問題在於 數組之間有長有短
跟上題思路一樣,兩指針,在兩個列表中,把小的依次寫在新數組中,當一個數組完畢後,直接把另一個數組剩餘的數字加在新數組中就行了

import java.util.*;
class Demo05_07{
    public static void main(String[] args){
        int[] list1={1,3,5,7,9};
        int[] list2={2,4,6,8,10};
        System.out.println(Arrays.toString(merge(list1,list2)));
    }
   
    public static int[] merge(int[] list1,int[] list2){
        if(list1==null&&list2==null){
            return null;
        }
        if(list1==null){
            return list2;
        }
        if(list2==null){
            return list1;
        }
        //只有兩個都不是null的情況再考慮具體操作
        int[] list3=new int[list1.length+list2.length];
        int p1=0;
        int p2=0;
        int p3=0;
        while(true){
            if(p1==list1.length&&p2==list2.length){
                break;
            }
            if(p1<list1.length&&p2==list2.length){
                list3[p3++]=list1[p1++];
            }else if(p1==list1.length&&p2<list2.length){
                list3[p3++]=list2[p2++];
            }else{
                if(list1[p1]<=list2[p2]){
                    list3[p3++]=list1[p1++];
                }else{
                    list3[p3++]=list2[p2++];
                }
            }
        }
        return list3;
    }
}

Demo05_08

在這裏插入圖片描述
這個題需要的方法很多你要盯緊步驟
1.創建一個單詞表
2.隨機從單詞表中抽取一個單詞
3.創建一個該單詞的狀態表 默認值是false(密文)
4.開始猜一個單詞
5.根據單詞和狀態表 決定密文形式
6.輸出密文並提示用戶輸入字母
7.判斷單詞中是否有該字母
8.改變單詞狀態表 已修改/未修改
9.是否結束
10.最後再去做多單詞猜測
這就是這個題的步驟,
下面是代碼:

import java.util.*;
class Demo05_08{
   
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        //1.創建一個單詞表
        String[] words={"naruto","kakashi","sasuke","banana","java","program"};
        //10.最後再去做多單詞猜測
        while(true){
            //2.隨機從單詞表中抽取一個單詞
            String word=words[random.nextInt(words.length)];
            //3.創建一個該單詞的狀態表 默認值是false(密文)
            boolean[] status=new boolean[word.length()];
            int miss=0; //猜錯的個數
            //4.開始猜一個單詞
            while(true){
                //5.根據單詞和狀態表 決定密文形式
                String ciphertext=getCipherText(word,status);
                //6.輸出密文並提示用戶輸入字母
                System.out.print("Enter a letter in word "+ciphertext+" >");
                char letter=scanner.nextLine().charAt(0);//"p".charAt(0)
                //7.判斷單詞中是否有該字母
                if(isContainsLetter(word,letter)){
                    //8.改變單詞狀態表 已修改/未修改 
                    //true 表示從未修改 第一次來的
                    //false 表示已修改  不是第一次來 提示已經存在
                    if(!changeWordStatus(word,status,letter)){
                        System.out.println("\t "+letter+" is already in the word");
                    }
                }else{
                    System.out.println("\t "+letter+" is not in the word");
                    miss++;
                }
                //9.是否結束
                if(isFinish(status)){
                    System.out.println("The word is "+word+". You miss "+miss+" time");
                    break;
                }
            }
            System.out.print("Do you want to guess another word?Enter y or n:");
            String choice=scanner.nextLine();
            if(choice.equals("n")){
                System.out.println("Welcome!Thank you! FUCK PROGRAM!");
                break;
            }
        }
    }
    public static boolean isFinish(boolean[] status){
        for(int i=0;i<status.length;i++){
            if(!status[i]){
                return false;
            }
        }
        return true;
    }
    public static boolean changeWordStatus(String word,boolean[] status,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                if(status[i]){
                    return false;   //說明已經修改
                }else{
                    status[i]=true;
                }
            }
        }
        return true;
    }
    public static boolean isContainsLetter(String word,char letter){
        for(int i=0;i<word.length();i++){
            if(word.charAt(i)==letter){
                return true;
            }
        }
        return false;
    }
    public static String getCipherText(String word,boolean[] status){
        String ciphertext="";
        for(int i=0;i<status.length;i++){
            if(status[i]){
                ciphertext+=word.charAt(i);
            }else{
                ciphertext+="*";
            }
        }
        return ciphertext;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章