有關數組的題目

import java.util.Scanner;
class Demo01{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter the integers between 1 and 100:");
        int[] arr=new int[0];
        /*
        給數組賦值的整個過程
         */
        while(true){
            int num=input.nextInt();
            if(num==0){
                break;
            }
            arr=copyOf(arr);
            arr[arr.length-1]=num;
            
        }
        /*
        冒泡排序
         */
        paiXu(arr);
        /*
        打印排序之後的數組
        */
        show(arr);
        /*
        記錄次數的整個過程
         */
        ciShu(arr);
    }
    /*
    數組擴容
     */
    public static int[] copyOf(int[] arr){
        int[] newArr=new int[arr.length+1];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        return newArr;
    }
    /*
    冒泡排序
    */
    public static void paiXu(int[] arr){
        int temp1;
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr.length-1;j++){
                if(arr[j]>arr[j+1]){
                    temp1=arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp1;
                }
            }
        }
    }
    /*
    記錄次數的整個過程
    */
    public static void ciShu(int[] arr){
        int temp2=1;
        for(int j=0;j<arr.length-1;j++){
            if(j<arr.length-2){
                if(arr[j]!=arr[j+1]){
                    System.out.println(arr[j]+" occurs "+temp2+" times");
                    temp2=1;
                }else{
                    temp2++;
                }
            }else{
                if(arr[j]==arr[j+1]){
                    temp2++;
                    System.out.println(arr[j]+" occurs "+temp2+" times");
                }else{
                    System.out.println(arr[j+1]+" occurs "+temp2+" times");
                }
            }
            
        }
    }
    /*
    打印數組
     */
    public static void show(int[] arr){
        for(int i=0;i<arr.length;i++){
            System.out.print(arr[i]+" ");
        }
        System.out.println();
    }
}

 

import java.util.Scanner;
class Demo02{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter ten number:");
        int[] arr1=new int[10];
        int[] arr2=new int[10];//備用
        int count1=0,count2=1,count3=0;
        while(true){
            int num=input.nextInt();
            arr1[count1++]=num;
            if(count1==10){
                break;
            }
        }
        /*
        遍歷數組arr1
         */    
        for(int i=0;i<count1;i++){
            if(i==0){
                arr2[count3++]=arr1[i];
            }else{
                for(int j=0;j<count3;j++){
                    if(arr1[i]==arr2[j]){
                        count2++;
                    }
                }
                if(count2==1){
                    arr2[count3++]=arr1[i];
                }
                count2=1;
            }
            
        }
        System.out.println("The number of distinct number:"+count3);
        System.out.print("The distinct numbers are:");
        for(int i=0;i<count3;i++){
            System.out.print(arr2[i]+" ");
        }
        System.out.println();
    }
}

 

import java.util.Scanner;
class Demo04{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter a balls:");
        int balls=input.nextInt();
        System.out.print("Enter a slots:");
        int slots=input.nextInt();
        int[] slot=new int[slots];
        String str="";
        //int count=0;
        for(int i=1;i<=balls;i++){
            str=random(slots);
            System.out.println(str);
            slot[getCount(str)]++;
        }
        for(int i=0;i<slot.length;i++){
            System.out.print(slot[i]+" ");
        }
    }
    public static String random(int slots){
        String str="";
        for(int j=0;j<slots-1;j++){
            if((int)(Math.random()*2)==0){
                str=str+"L";
            }else{
                str=str+"R";
            }
        }
        return str;
    }
    public static int getCount(String str){
        int count=0;
        for(int l=0;l<str.length();l++){
            if(str.charAt(l)=='R'){
                count++;
            }
        }
        return count;
    }
}

 

import java.util.Scanner;
class Demo08{
    public static void main(String[] args){
        Scanner input=new Scanner(System.in);
        System.out.print("Enter strings:");
        String[] str=new String[0];
        String word="";
        int count1=0,count2=0;
        while(true){
            String str1=input.next();
            /*
            記錄輸入字符的個數
             */
            count1++;
            str=copyOf(str);
            str[str.length-1]=str1;
            if(count1==4){
                break;
            }
        }
        word=str[(int)(Math.random()*str.length)];
        //存放密文
        char[] ciphertext=new char[word.length()];
        //將密文轉化爲字符串
        String string="",string1="";
        string1=word;
        for(int i=0;i<word.length();i++){
            ciphertext[i]='*';
        }
        while(true){
            while(true){
                System.out.print("Enter a letter in word:");
                for(int i=0;i<ciphertext.length;i++){
                    System.out.print(ciphertext[i]);
                }
                System.out.print("->");
                //存放用戶猜測的字符
                char c=input.next().charAt(0);
                if(isExisten(ciphertext,c)){
                    System.out.println(c+" is already in the word");
                }else{
                    /*
                    判斷該字符是否出現
                    */
                    if(!isExistence(c,word,ciphertext)){
                        /*
                        記錄輸入錯誤的次數
                        */
                        count2++;
                        System.out.println(c+" is not already in the word ");
                    }
                }
                for(int i=0;i<ciphertext.length;i++){
                    string+=ciphertext[i];
                }
                /*
                是否結束
                */
                if(string.equals(word)){
                    break;
                }
                string="";
            }
            /*
            重新賦值爲空字符串
             */
            string=bulid();
            System.out.println("The word is program,You missed "+count2+" times");
            System.out.print("Do you want to guess another word?Enter y or n:");
            char inputChar=input.next().charAt(0);
            if(inputChar=='y'){
                word=str[(int)(Math.random()*str.length)];
                ciphertext=printText(word);
                for(int i=0;i<word.length();i++){
                    ciphertext[i]='*';
                }
                count2=0;
            }else{
                break;
            }
        }
    }
    /*
    重新創建一個空串
     */
    public static String bulid(){
        String string="";
        return string;
    }
    /*
    密文更新
     */
    public static char[] printText(String str){
        char[] newCiphertext=new char[str.length()];
        return newCiphertext;
    }
    /*
    數組的擴容
     */
    public static String[] copyOf(String[] str){
        String[] newStr=new String[str.length+1];
        for(int i=0;i<str.length;i++){
            newStr[i]=str[i];
        }
        return newStr;
    }
    /*
    數組的輸出
     */
    public static void show(String[] str){
        for(int i=0;i<str.length;i++){
            System.out.print(str[i]+" ");
        }
        System.out.println();
    }
    /*
    判斷該字符是否出現
     */
    public static boolean isExistence(char c,String word,char[] ciphertext){
        int count=0;
        char[] arrC=word.toCharArray();
        for(int i=0;i<arrC.length;i++){
            if(arrC[i]==c){
                ciphertext[i]=c;
                count++;
            }
        }
        if(count>=1){
            return true;
        }else{
            return false;
        }
    }
    /*
    遍歷密文中是否存在該字符
     */
    public static boolean isExisten(char[] ciphertext,char c){
        int count=0;
        for(int i=0;i<ciphertext.length;i++){
            if(ciphertext[i]==c){
                count++;
            }
        }
        if(count>=1){
            return true;
        }else{
            return false;
        }
    }
}

運行結果:

Enter strings:write that program take
Enter a letter in word:*****->w
Enter a letter in word:w****->t
Enter a letter in word:w**t*->r
Enter a letter in word:wr*t*->i
Enter a letter in word:writ*->e
The word is program,You missed 0 times
Do you want to guess another word?Enter y or n:y
Enter a letter in word:*******->p
Enter a letter in word:p******->r
Enter a letter in word:pr**r**->p
p is already in the word
Enter a letter in word:pr**r**->o
Enter a letter in word:pro*r**->g
Enter a letter in word:progr**->n
n is not already in the word
Enter a letter in word:progr**->m
Enter a letter in word:progr*m->a
The word is program,You missed 1 times
Do you want to guess another word?Enter y or n:n

 

 

 

 

 

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