首页 > 其他分享 >26-稀疏数组

26-稀疏数组

时间:2023-02-07 14:46:53浏览次数:33  
标签:11 26 int 稀疏 System ++ 数组 out

0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
有效值数量:2
11 11 2
1 2 1
2 3 2

package com.wang.array;

public class Demo09 {
    public static void main(String[] args) {
        
    int[][] a = new int[11][11];
    a[1][2]=1;
    a[2][3]=2;
        for (int[] ints : a) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();//输出原始数组
        }

        int sum=0;                            //提取有效值数量
        for (int i = 0; i < 11; i++) {
            for (int i1 = 0; i1 < 11; i1++) {
                if(a[i][i1]!=0){
                    sum++;
                }
            }
        }
        System.out.println("有效值数量:"+sum);  //提取有效值数量

        ////////创建一个稀疏数组
        int[][] b = new int[sum+1][3];

        b[0][0]=11;  //确定表头
        b[0][1]=11;
        b[0][2]=sum;

        //遍历二维数组,将非零值存放稀疏数组中
        int count = 0;
        for (int i = 0; i < a.length; i++) {
            for (int k = 0; k < a[i].length; k++) {
                if(a[i][k]!=0){
                    count++;
                    b[count][0]=i;
                    b[count][1]=k;
                    b[count][2]=a[i][k];
                }
            }
        }
        
        //输出稀疏数组
        for (int[] ints : b) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }

///////////////////还原稀疏数组
        int[][] c =new int[b[0][0]][b[0][1]]; //定义数组行列数
        for (int i = 1; i < b.length; i++) {
            c[b[i][0]][b[i][1]] = b[i][2];
        }
        
        //打印
        for (int[] ints : c) {
            for (int anInt : ints) {
                System.out.print(anInt+"\t");
            }
            System.out.println();
        }

    }
}

标签:11,26,int,稀疏,System,++,数组,out
From: https://www.cnblogs.com/PedroPascal/p/17098320.html

相关文章

  • PHP对数组的高级遍历和操作处理方法
    PHP对数组的处理可以称为该语言最有吸引力的特性之一,它支持70多种数组相关的函数。不论你想翻转一个数组、判断某个值在数组中是否存在、将数组转换成一个字符串还是计算数......
  • 1.6 通过地址和索引实现数组
    CPU把基址寄存器+变址寄存器的值解释为实际查看的内存地址。变址寄存器的值就相当于高级编程语言程序中数组的索引功能。数组是指同样长度的数据在内存中进行连续排列的数......
  • POJ 3267 The Cow Lexicon
    TheCowLexiconTimeLimit: 2000MS MemoryLimit: 65536KTotalSubmissions: 11349 Accepted: 5432DescriptionFewknowthatthecowshavetheirowndictionar......
  • PHP数组使用、特性、函数的总结
    包含其他数组的数组成为多维数组关联数组(即非数字索引数组)使用更有意义的数据作为数组的索引1、数组索引数组$products = array(‘a’, ‘b’, ‘c’);//就像一个......
  • 数组扩容与数组拷贝
    前言在上一篇文章中,千锋壹哥给大家讲解了数组的创建、初始化及遍历方式,这些是我们学习数组的基础。其实数组的内容非常多,今天这篇文章,千锋壹哥主要带大家学习数组的扩容、......
  • 数组的find方法 ES6 230207
    数组的find方法接收一个方法体方法体收一个参数这个参数作为数组的成员返回一个布尔值如果布尔值的结果为真那么查找成功并中止继续下一个成员的查找......
  • 数组的findIndex方法 ES6 230207
    用法示例查找出数据中成员大于3的数据的下标......
  • poj 2262 Goldbach's Conjecture 素数打表
    Goldbach'sConjectureTimeLimit: 1000MS MemoryLimit: 65536KTotalSubmissions: 48891 Accepted: 18614DescriptionIn1742,ChristianGoldbach,aGermanamate......
  • POJ 3126 Prime Path 素数+BFS
    PrimePathTimeLimit: 1000MS MemoryLimit: 65536KTotalSubmissions: 27754 Accepted: 15152DescriptionTheministersofthecabinetwerequiteupsetbythem......
  • POJ3263 Tallest Cow 括号技巧
    题目描述FJ'sN(1≤N≤10,000)cowsconvenientlyindexed1..Narestandinginaline.Eachcowhasapositiveintegerheight(whichisabitofsecret).You......