第八章第十三题(定位最大的元素)(Locate the largest element)

第八章第十三题(定位最大的元素)(Locate the largest element)

  • *8.13(定位最大的元素)编写下面的方法,返回二维数组中最大元素的位置。
    public static int[] locateLargest(double[][] a)
    返回值是包含两个元素的一维数组。这两个元素表示二维数组中最大元素的行下标和列下标。编写一个测试程序,提示用户输入一个二维数组,然后显示这个数组中最大元素的位置。下面是一个运行示例:
    Enter the number of rows and columns of the array:3 4
    Enter the array:
    23.5 35 2 10
    4.5 3 45 3.5
    35 44 5.5 9.6
    The location of the largest element is at (1,2)
    *8.13(Locate the largest element)Write the following method to return the position of the largest element in a two-dimensional array.
    public static int[] locateLargest(double[][] a)
    The return value is a one-dimensional array of two elements. These two elements represent the row and column subscripts of the largest element in a two-dimensional array. Write a test program, prompt the user to input a two-dimensional array, and then display the position of the largest element in the array. Here is a running example:
    Enter the number of rows and columns of the array:3 4
    Enter the array:
    23.5 35 2 10
    4.5 3 45 3.5
    35 44 5.5 9.6
    The location of the largest element is at (1,2)

  • 参考代码:

    package chapter08;
    
    import java.util.Scanner;
    
    public class Code_13 {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            System.out.print("Enter the number of rows and columns of the array:");
            int rows = input.nextInt();
            int columns = input.nextInt();
            System.out.println("Enter the array:");
            double[][] array = new double[rows][columns];
    
            for (int i = 0; i < array.length; i++) {
                for (int j = 0; j < array[i].length; j++) {
                    array[i][j] = input.nextDouble();
                }
            }
    
            int[] largestLocation=locateLargest(array);
            System.out.println("The location of the largest element is at ("+largestLocation[0]+","+largestLocation[1]+")");
        }
    
        public static int[] locateLargest(double[][] a) {
            int[] max = new int[2];
    
            double largest=a[0][0];
            for(int i=0;i<a.length;i++){
                for(int j=0;j<a[i].length;j++){
                    if(largest<a[i][j]){
                        largest=a[i][j];
                        max[0]=i;
                        max[1]=j;
                    }
                }
            }
    
    
            return max;
        }
    }
    
    
  • 结果显示:

    Enter the number of rows and columns of the array:3 4 
    Enter the array:
    23.5 35 2 10
    4.5 3 45 3.5
    35 44 5.5 9.6
    The location of the largest element is at (1,2)
    
    Process finished with exit code 0
    
    
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值