05.第八章:多维数组

创建二维数组
二维数组长度

1. 二维数组的处理

  • 使用输入值初始化数组
//1.使用输入值初始化数组
    public void initArray(int array[][]){
        Scanner input=new Scanner(System.in);
        for(int row=0;row<array.length;row++){
            for(int colum=0;colum<array[row].length;colum++){
                array[row][colum]=input.nextInt();
            }
        }

    }
  • 数组按列求和
 //2.数组按列求和
    public static void sumbycolum(int array[][]){
        for(int colum=0;colum<array[0].length;colum++) {
            int total = 0;
            for(int row=0;row<array.length;row++){
                total=total+array[row][colum];
            }
            System.out.println("第"+(colum+1)+"列的和为"+total);
        }
    }

  • 哪一行的和最大
//3.哪一行的和最大
    public static void  maxRow(int array[][]){
        int maxrow=0;
        int indexOfMaxRow=0;
        for(int colum=0;colum<array[0].length;colum++)
            maxrow=maxrow+array[0][colum];
        for(int row=1;row<array.length;row++){
            int totalOfthisRow=0;
            for(int colum=0;colum<array[row].length;colum++){
                totalOfthisRow=totalOfthisRow+array[row][colum];
                if(totalOfthisRow>maxrow){
                    maxrow=totalOfthisRow;
                    indexOfMaxRow=row;
                }
            }
        }
        System.out.print("Row "+(indexOfMaxRow+1)+" has the max sum of "+maxrow);
    }

2. 示例学习

  • 找出距离最近的点对
使用二维数组表示点
public class text2_距离最近的点对 {
    public static void main(String[] args){
        System.out.println("输入有几个点:");
        Scanner input=new Scanner(System.in);
        int row=input.nextInt();
        double [][]array=new double[row][2];
        System.out.println("输入所有点的x,y坐标:");
        for(int i=0;i<row;i++) {
            array[i][0]=input.nextDouble();
            array[i][1]=input.nextDouble();
        }
        //定义变量p1,p2表示距离最近的点所在的行
        int p1=0,p2=1;
        double shortDistance=distance(array[p1][0],array[p1][1],array[p2][0],array[p2][1]);
        //从0开始,每一个和后面其余所有行求距离
        for(int i=0;i<array.length-1;i++)
        {
            for(int j=i+1;j<array.length;j++)
            {
                double distance=distance(array[i][0],array[i][1],array[j][0],array[j][1]);
                if(distance<shortDistance)
                {
                    p1=i;
                    p2=j;
                    shortDistance=distance;
                }
            }
        }

        System.out.println("最短的两点是("+array[p1][0]+","+array[p1][1]+")("+array[p2][0]+","+array[p2][1]+")"+"\n最短的距离为:"+shortDistance);
    }

    private static double distance(double x1, double y1, double x2, double y2) {

        return Math.sqrt(Math.pow((x2-x1),2)+Math.pow((y2-y1),2));
    }

}

  • 数独
    数独是一个 9 x 9 的网格 , 它被分为更小的 3 x 3 的盒子 ( 也称为区域或者块 ) , 如图
    8 - 4 a 所示 。 将从 1 到 9 的数字植人一些称为固定方格 ( fixed cell ) 的格子里 。 该程序的目标是将从 1 到 9 的数字植人那些称为自由方格 ( free cell ) 的格子 , 以便能够使得每行每列以及每个 3 x 3 的盒子都包含从 1 到 9 的数字 。

    一旦找到一个数独难题的解决方案 , 如何验证它是正确的呢?
    检査每个单元格 。 每个单元格必须是 1 到 9 的数字 , 单元格数字在每行 、 每列 , 以及
    每个小方盒中都是唯一的 。

public class text3_数独 {
    public static void main(String[] args){
        int [][]grid=readASolution();
        System.out.println(isValid(grid)?"Valid solution":"Invalid solution");
    }

    private static int[][] readASolution() {
        Scanner input=new Scanner(System.in);
        System.out.println("请输入结果:");
        int [][]grid=new int[9][9];
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
                grid[i][j]=input.nextInt();
        return grid;
    }
    
    private static boolean isValid(int [][]grid){
        //判断每一个数字是不是符合规则
        for(int i=0;i<9;i++)
            for(int j=0;j<9;j++)
                if(grid[i][j]<1||grid[i][j]>9||!isValid(i,j,grid))
                    return false;
        return true;
        
    }

    //验证i行j列的数字,在该行是不是唯一的,在该列是不是唯一的,在3*3的方格中是不是唯一的
    private static boolean isValid(int i, int j, int[][] grid) {
        //验证在i行是不是唯一的
        for(int column=0;column<9;column++)
            if(column!=j&&grid[i][column]==grid[i][j])
                return false;
        //验证在j列是不是唯一
        for(int row=0;row<9;row++)
            if(row!=i&&grid[row][j]==grid[i][j])
                return false;
        
        //验证在3*3的格子里是不是唯一的
        for(int row=(i/3)*3;row<(i/3)*3+3;row++){
            for(int col=(j/3)*3;col<(j/3)*3+3;col++)
                if(row!=i&&col!=j&&grid[row][col]==grid[i][j])
                    return false;
        }
        
        return true;
    }


}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容