Java练习题五【数组】

目录

练习题53.计算数字的出现次数

练习题54.打印不同的数

练习题55.是否排好序了

练习题56.游戏:豆机

练习题57.完全相同的数组

练习题58.模式识别:四个连续相等的数

练习题59.合并两个有序列表

练习题60.游戏:猜字游戏

练习题61.求矩阵中各列数字的和

练习题62.求矩阵主对角线元素的和

练习题63.两个矩阵相乘

练习题64.探索矩阵

练习题65.连续的四个相等的数


 


练习题53.计算数字的出现次数

import java.util.Scanner;
class Demo53{
    public static void main(String[] args){
        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"));
            }
        }
    }
}

练习题54.打印不同的数

import java.util.*;
class Demo54{
    public static void main(String[] args){
        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 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;
    }
}

练习题55.是否排好序了

import java.util.*;
class Demo55{
    public static void main(String[] args){
        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();
        }
        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;
    }
}

练习题56.游戏:豆机

import java.util.*;
class Demo56{
    public static void main(String[] args){
        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();
        int[] arr=new int[slots];
        for(int i=0;i<balls;i++){
            String path=getPath(slots);
            System.out.println(path);
            arr[getR(path)]++;
        }
        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){
        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;
    }
}

练习题57.完全相同的数组

class Demo57{
    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){
        if(list1.length!=list2.length){
            return false;
        }
        for(int i=0;i<list1.length;i++){
            if(list1[i]!=list2[i]){
                return false;
            }
        }
        return true;
    }
}

练习题58.模式识别:四个连续相等的数

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

练习题59.合并两个有序列表

import java.util.*;
class Demo59{
    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;
        }
        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;
    }
}

练习题60.游戏:猜字游戏

import java.util.*;
class Demo60{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        Random random=new Random();
        String[] words={"naruto","kakashi","sasuke","banana","java","program"};
        while(true){
            String word=words[random.nextInt(words.length)];
            boolean[] status=new boolean[word.length()];
            int miss=0;
            while(true){
                String ciphertext=getCipherText(word,status);
                System.out.print("Enter a letter in word "+ciphertext+" >");
                char letter=scanner.nextLine().charAt(0);
                if(isContainsLetter(word,letter)){
                    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++;
                }
                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;
    }
}

练习题61.求矩阵中各列数字的和

import java.util.*;
class Demo61{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.println("Enter numbers:");
        double[][] matrix=new double[3][4];
        for(int i=0;i<matrix.length;i++){
            for(int j=0;j<matrix[i].length;j++){
                matrix[i][j]=scanner.nextDouble();
            }
        }
        for(int col=0;col<matrix[0].length;col++){
            System.out.println(sumColumn(matrix,col));
        }
    }
    public static double sumColumn(double[][] m,int col){
        double sum=0;
        for(int row=0;row<m.length;row++){
            sum+=m[row][col];
        }
        return sum;
    }
}

练习题62.求矩阵主对角线元素的和

class Demo62{
    public static void main(String[] args){
        int[][] m={
            {1, 2, 3, 4},
            {5, 6, 7, 8},
            {9, 10,11,12},
            {13,14,15,16}
        };
        int sum=0;
        for(int i=0;i<m.length;i++){
            sum+=m[i][i];
        }
        System.out.println(sum);
        int subsum=0;
        for(int i=0;i<m.length;i++){
            subsum+=m[i][m.length-1-i];
        }
        System.out.println(subsum);
    }
}

练习题63.两个矩阵相乘

class Demo63{
    public static void main(String[] args){
        double[][] A={ 
            {1,2,3},
            {4,5,6},
            {7,8,9}
        };
        double[][] B={ 
            {0,2.0,4.0},
            {1,4.5,2.2},
            {1.1,4.3,5.2}
        };
        double[][] C=new double[A.length][B[0].length];
        for(int i=0;i<C.length;i++){
            for(int j=0;j<C[i].length;j++){
                double sum=0;
                for(int k=0;k<B.length;k++){
                    sum+=A[i][k]*B[k][j];
                }
                C[i][j]=sum;
                System.out.print(C[i][j]+" ");
            }
            System.out.println();
        }
    }
}

练习题64.探索矩阵

import java.util.*;
class Demo64{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter size:");
        int size=scanner.nextInt();
        int[][] m=new int[size][size];
        Random random=new Random();
        for(int i=0;i<size;i++){
            for(int j=0;j<size;j++){
                m[i][j]=random.nextInt(2);
                System.out.print(m[i][j]+" ");
            }
            System.out.println();
        }
        checkRow(m);
        checkCol(m);
        checkDiagonal(m);
        checkSubDiagonal(m);
    }
    public static void checkSubDiagonal(int[][] m){
        int sum=0;
        for(int i=0;i<m.length;i++){
            sum+=m[i][m.length-1-i];
        }
        if(sum==m.length||sum==0){
            System.out.printf("副主对角线全相等且是%d\n",sum==0?0:1);
        }
    }
    public static void checkDiagonal(int[][] m){
        int sum=0;
        for(int i=0;i<m.length;i++){
            sum+=m[i][i];
        }
        if(sum==m.length||sum==0){
            System.out.printf("主对角线全相等且是%d\n",sum==0?0:1);
        }
    }
    public static void checkRow(int[][] m){
        for(int i=0;i<m.length;i++){
            int sum=0;
            for(int j=0;j<m[i].length;j++){
                sum+=m[i][j];
            }
            if(sum==m.length||sum==0){
                System.out.printf("第%d行全相等且是%d\n",i+1,sum==0?0:1);
            }
        }
    }
    public static void checkCol(int[][] m){
        for(int j=0;j<m.length;j++){
            int sum=0;
            for(int i=0;i<m.length;i++){
                sum+=m[i][j];
            }
            if(sum==m.length||sum==0){
                System.out.printf("第%d列全相等且是%d\n",j+1,sum==0?0:1);
            }
        }
    }
}

练习题65.连续的四个相等的数

import java.util.*;
class Demo65{
    public static void main(String[] args){
        Scanner scanner=new Scanner(System.in);
        System.out.print("Enter row ,col:");
        int row=scanner.nextInt();
        int col=scanner.nextInt();
        int[][] m=new int[row][col];
        for(int i=0;i<row;i++){
            for(int j=0;j<col;j++){
                m[i][j]=scanner.nextInt();
            }
        }
        System.out.println(isConsecutiveFour(m));
    }
    public static boolean isConsecutiveFour(int[][] m){
        for(int i=0;i<m.length;i++){
            for(int j=0;j<m[i].length;j++){
                if(j<=m[i].length-4){
                    boolean flag=true;
                    for(int c=j+1;c<=j+3;c++){
                        if(m[i][j]!=m[i][c]){
                            flag=false;
                            break;
                        }
                    }
                    if(flag){
                        return true;
                    }
                }
                if(i<=m.length-4){
                    boolean flag=true;
                    for(int r=i+1;r<=i+3;r++){
                        if(m[i][j]!=m[r][j]){
                            flag=false;
                            break;
                        }
                    }
                    if(flag){
                        return true;
                    }
                }
                if(i<=m.length-4&&j<=m[0].length-4){
                    boolean flag=true;
                    for(int r=i+1,c=j+1;r<=i+3;r++,c++){
                        if(m[i][j]!=m[r][c]){
                            flag=false;
                            break;
                        }
                    }
                    if(flag){
                        return true;
                    }
                }
                if(i>=3&&j<=m[0].length-4){
                    boolean flag=true;
                    for(int r=i-1,c=j+1;c<=j+3;r--,c++){
                        if(m[i][j]!=m[r][c]){
                            flag=false;
                            break;
                        }
                    }
                    if(flag){
                        return true;
                    }
                }
            }
        }
        return false;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值