Java Programming Training Problems (Arr)

A topic

import java.util.Scanner;
class Demo05_01_02{
    public static void main(String[] args){
        //0 1 2 3 4 5 ~ 100
        //0 单独不算
        //arr[i] 就表示数字i出现的次数
        int[] arr=new int[101];
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter numbers:");
        while(true){
            int num=scanner.nextInt();
            if(num==0){
                break;
            }
            arr[num]++;
        }
        for(int i=0;i<arr.length;i++){
            if(arr[i]!=0){
                System.out.println(i+" occurs "+arr[i]+(arr[i]>1?" times":" time"));
            }
        }
    }
}

Topic two

 

Ideas:

The first:

  1. First of all input
  2. In determining whether only appears once, judging method method1 method2 method3 

The second: determining at each time entry input

import java.util.*;
class Demo05_02{
    public static void main(String[] args){
        /*
        思路1
            在全部输入之后去重复 func1
        思路2
            边输入边去重复 func2
        */
        // func1();
        func2();
    }
    public static void func2(){
        int[] arr=new int[0];
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter numbers:");
        for(int i=0;i<10;i++){
            int num=scanner.nextInt();
            if(!contains(arr,num)){
                arr=copyOf(arr,arr.length+1);
                arr[arr.length-1]=num;
            }
        }
        System.out.println(Arrays.toString(arr));
    }
    public static void func1(){
        //1.循环遍历数组进行赋值
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter numbers:");
        int[] arr = new int[10];
        for(int i = 0;i < arr.length;i++){
            arr[i] = scanner.nextInt();
        }
        //2.开始对已有的数据进行去重复操作
        // 1 2 3 3 2 4 3 2 4 1
        // 1 2 3 4
        // method1(arr);        //空间S(n) 时间O(nm)
        // method2(arr);        //空间S(1) 时间O(n^2)
        // method3(arr);
    }
    public static void method3(int[] arr){
        //不创建额外空间 不许改变原先的顺序
        int i=0;
        int size=arr.length;
        while(i<size){
            for(int j=i+1;j<size;){
                if(arr[j]==arr[i]){
                    for(int k=j+1;k<size;k++){
                        arr[k-1]=arr[k];
                    }
                    size--;
                }else{
                    j++;
                }
            }
            i++;
        }
        for(i=0;i<size;i++){
            System.out.print(arr[i]+" ");
        }
    }
    public static void method2(int[] arr){
        //插入排序
        for(int i=1;i<arr.length;i++){
            int e=arr[i];
            int j;
            for(j=i;j>0&&arr[j-1]>e;j--){
                arr[j]=arr[j-1];
            }
            arr[j]=e;
        }
        //连续相等
        for(int i=0;i<arr.length;){ //O(n)
            System.out.print(arr[i]+" ");
            int count=1;
            for(int j=i+1;j<arr.length;j++){
                if(arr[j]==arr[i]){
                    count++;
                }else{
                    break;
                }
            }
            i+=count;
        }
    }
    public static void method1(int[] arr){
        int[] newArr=new int[0];
        for(int i=0;i<arr.length;i++){ //O(n)
            if(!contains(newArr,arr[i])){ //O(m)
                newArr=copyOf(newArr,newArr.length+1);
                newArr[newArr.length-1]=arr[i];
            }
        }
        System.out.println(Arrays.toString(newArr));
    }
    public static boolean contains(int[] arr,int key){
        for(int i=0;i<arr.length;i++){
            if(arr[i]==key){
                return true;
            }
        }
        return false;
    }
    public static int[] copyOf(int[] arr,int newLen){
        int[] newArr=new int[newLen];
        for(int i=0;i<arr.length;i++){
            newArr[i]=arr[i];
        }
        return newArr;
    }

}

Topic three

 

Thinking: prompt the user for an array, the array is determined whether the number ordered, if the output "This orderly array", if not then output "of the present disordered array"

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;
    }

}

Title four

Idea: each ball has a chance to stake a left or right fifty percent, we can create two random numbers to represent the left and right, but there are problems found, R which fall into the bottle with a path the quantity.

import java.util.*;
class Demo05_04{
    /*
    输入的数据:槽子的个数 球的个数=路径的个数
               创建槽子的具体的容器int[]
               每一个小球下落的路径L R 字符串
               对于每一个小球而言其路径中的步骤是随机产生L R
    1.提示用户输入槽子的个数和小球的个数
    2.根据已有的槽子的个数去创建槽子容器
    3.根据已有的球和槽子的个数去随机创建一个小球下落的路径
    4.路径中经过几个钉子?路径的步骤有几步 和槽子的个数有关
    5.如何通过路径的经过得知最终所落入的槽子?
    */
    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;
    }
}

Title five

Ideas: First, the user is prompted array length, in each position of the input array of numbers, in front to back is determined whether there occurs four consecutive numbers

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("没有!");
    }
}

Title six 

Ideas: first determine whether two arrays are empty, if a merger is empty then out of the array is another array, if not empty it is determined that the size of the numbers until the end of the two arrays are.

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;
    }
}

Title seven

Idea: to create a few words, then randomly select one, you encrypt it, prompting the user, if the letter of the word is not entered, note the error once, if the letters have been entered, suggesting that letter has occurred, when the user after guess the word, ask whether to continue guessing, if it continues, then start again.

import java.util.*;
class Demo05_08{
    /*
    数据 一组单词的明文  单词的密文  单词的状态
    program
    1000000 r r
    pr**r**
    */
    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;
    }
}

An array of topics being first here, plus follow-up, thank you browse.

Published 12 original articles · won praise 1 · views 233

Guess you like

Origin blog.csdn.net/Yi_nian_yu_dian/article/details/104379201