当前位置:编程学习 > JAVA >>

求个二维数组的算法

--------------------编程问答-------------------- 希望楼下能看明白,总之我是看不明白。
--------------------编程问答--------------------
public static void main(String[] args) {
int arr[][]={
{1,1,0,1,0,1,1,1,0,1},
{1,0,1,0,1,0,1,0,1,0},
{1,1,0,1,1,1,1,1,0,1},
{1,0,0,1,0,1,1,1,0,0},
{1,1,1,1,1,1,1,1,1,1},
};
 int  numrow[][] = new int [5][5];
 int  numcol[][]=new int[3][10];
/*求个算法 (arr五行十列)
 *  行:从arr[0[0]]到arr[0][9]如果值是1则累计和然后赋给numrow[0][],如果为0跳过不累计
 * 
 * 最后numrow[5][5]={
 *  {2,1,3,1,0},
 * {1,1,1,1,1},
 * {2,5,1,0,0},
 * {1,1,3,0,0},
 * {10,0,0,0,0},
 *  }
 * 
 * 
 * 列:从arr[0[0]]到arr[4][0]如果值是1则累计和然后赋给numcol[0][],如果为0跳过不累计
 * 最后numcol[3][10]={
 *  {5,1,1,1,2,1,5,1,1,1},
 * {0,1,1,3,1,3,0,3,1,1},
 *       {0,1,0,0,0,0,0,0,0,1},
 *  }
 * 
 * 
 * 遍历arr一次就把numrow numcol两个值算出来
 * **/
}


--------------------编程问答--------------------

public static void main(String[] args) {
        int arr[][]={
                {1,1,0,1,0,1,1,1,0,1},
                {1,0,1,0,1,0,1,0,1,0},
                {1,1,0,1,1,1,1,1,0,1},
                {1,0,0,1,0,1,1,1,0,0},
                {1,1,1,1,1,1,1,1,1,1},
                };
         int  numrow[][] = new int [5][5];
         int  numcol[][]=new int[3][10];
        
         int sum = 0;
         for(int i=0;i<5;i++){
          for(int j=0;j<10;j+=2){
          sum = 0;
          for(int k=j;k<10;k++){
          if(k!=j&&arr[i][k]==0)break;
          sum += arr[i][k];
          }
          numrow[i][j>>1] = sum;
          }
         }
         for(int j=0;j<10;j++){
          for(int i=0;i<5;i+=2){
          sum = 0;
          for(int k=i;k<5;k++){
          if(k!=i&&arr[k][j]==0)break;
          sum += arr[k][j];
          }
          numcol[i>>1][j] = sum;
          }
         }
         
         for(int i=0;i<5;i++){
          for(int j=0;j<5;j++){
          System.out.print("\t"+numrow[i][j]);
          }
          System.out.println();
         }
         for(int i=0;i<3;i++){
          for(int j=0;j<10;j++){
          System.out.print("\t"+numcol[i][j]);
          }
          System.out.println();
         }
    }
--------------------编程问答-------------------- 楼上个哥们。结果不对呀 --------------------编程问答-------------------- 错误的代码,不想做了,解决的思路应该是这样的。
因为只能遍历一次,所以比较麻烦。


package cn.dzr.number;


public class Number {
    public static void main(String[] args)
    {
        int arr[][]={
                {1,1,0,1,0,1,1,1,0,1},
                {0,1,0,1,0,1,1,0,1,0},
                {1,1,0,1,1,1,1,1,0,1},
                {1,0,0,1,0,1,1,1,0,0},
                {1,1,1,1,1,1,1,1,1,1},
                };
        
         int  numrow[][] = new int [5][5];
         int  numcol[][]=new int[3][10];
        
        int r = 0;      //表示行的统计数目
        int[] l = {0,0,0,0,0,0,0,0,0,0};      //表示列的统计数目
        
        int rsum = 0;   //行的初值
        
        int[] lsums = {0,0,0,0,0,0,0,0,0,0};   //列的初值
        
        
        for(int i=0;i<5;i++)
        {
            System.out.println(i+"XXXXXXXXX");
            rsum=0;
               r=0;
            for(int j=0;j<10;j++)
            {
                //处理行数据
                if(arr[i][j]==1)
                {
                    //如果当前数字为1,则需要分两种情况讨论
                    //1,rsum==0  则将rsum==1;
                    
                    //2, rsum>1; 则rsum++;
                    
                        rsum++;
                }
                
                if(arr[i][j]==0)
                {
                    //当前数字如果为0,则分两种情况讨论
                    //1, rsum==0 则不做处理,继续跳过
                    //2, rsum>0;则将rsum的值保存到numrow中。
                    if(rsum!=0)
                    {
                        numrow[i][r++]= rsum;
                        System.out.println(rsum);
                        rsum=0;
                    }
                }
                
//                //处理列
              if(i==0)
                  lsums[i]=arr[0][i];
              
               if(i>0)
              {
                    if(arr[i][j]==0)
                   {
                        //如果这个时候,lsum[j]为0,则什么都不做,跳过
                        //如果前面的数为>0,则将该值保存到 numcol[l[i]][i]中。同时 l[i]++;
                        if(lsums[j]>0)
                        {
                            System.out.println("l[i]="+l[i]+" i:"+i);
                            numcol[l[i]][j]=lsums[i];
                           lsums[i]=0;
                          l[i]++;
                            
                       }
                  }
                    
                    if(arr[i][j]==1)
                    {
                        //如果lsums[j]为0,则将该值赋值给lsum[j]
                       
                        lsums[j]++;
                       
                    }
                }
                
            }
        }
        
        out(numrow);
        System.out.println("---------------------------------");
        out(numcol);
    }
    
    public static void out(int[][] arrs)
    {
        for(int i=0;i<arrs.length;i++)
        {
            for(int j=0;j<arrs[i].length;j++)
            {
                System.out.print(arrs[i][j]+" ");
            }
            System.out.println();
        }
    }
}
--------------------编程问答-------------------- 上面的41行下面加
                         if(j==9)
                            numrow[i][r++]=rsum;

可以得到正确的解决行的统计问题。 --------------------编程问答-------------------- 这个应该可以了。我的时间,全部都浪费了。

package cn.dzr.number;


public class Number {
    public static void main(String[] args)
    {
        int arr[][]={
                {1,1,0,1,0,1,1,1,0,1},
                {0,1,0,1,0,1,1,0,1,0},
                {1,1,0,1,1,1,1,1,0,1},
                {1,0,0,1,0,1,1,1,0,0},
                {1,1,1,1,1,1,1,1,1,1},
                };
        
         int  numrow[][] = new int [5][5];
         int  numcol[][]=new int[3][10];
        
        int r = 0;      //表示行的统计数目
        int[] l = {0,0,0,0,0,0,0,0,0,0};      //表示列的统计数目
        
        int rsum = 0;   //行的初值
        
        int[] lsums = {0,0,0,0,0,0,0,0,0,0};   //列的初值
        
        
        for(int i=0;i<5;i++)
        {
           
            rsum=0;
               r=0;
            for(int j=0;j<10;j++)
            {
                //处理行数据
                if(arr[i][j]==1)
                {
                    //如果当前数字为1,则需要分两种情况讨论
                    //1,rsum==0  则将rsum==1;
                    
                    //2, rsum>1; 则rsum++;
                    
                        rsum++;
                        if(j==9)
                            numrow[i][r++]=rsum;
                }
                
                if(arr[i][j]==0)
                {
                    //当前数字如果为0,则分两种情况讨论
                    //1, rsum==0 则不做处理,继续跳过
                    //2, rsum>0;则将rsum的值保存到numrow中。
                    if(rsum!=0)
                    {
                        numrow[i][r++]= rsum;              
                        rsum=0;
                    }
                }
                
//                //处理列,当处理第一行的时候,将一的行的值都赋值给lsums。
             if(i==0)
                  lsums[j]=arr[0][j];
              
               if(i>0)
              {
                    if(arr[i][j]==0)
                   {
                        //如果这个时候,lsum[j]为0,则什么都不做,跳过
                        //如果前面的数为>0,则将该值保存到 numcol[l[i]][i]中。同时 l[i]++;
                        if(lsums[j]>0)
                        {
                            
                            numcol[l[j]][j]=lsums[j];
                            System.out.println("遇到a["+i+"]["+j+"]"+"将第"+l[i]+"行第"+j+"列赋值为"+lsums[i]);
                           lsums[j]=0;
                            l[j]++;
                            
                       }
                  }
                    
                    if(arr[i][j]==1)
                    {
                        //如果lsums[j]为0,则将该值赋值给lsum[j]
                       
                        lsums[j]++;
                        if(i==4)
                        {
                            //赋值
                            numcol[l[j]][j]=lsums[j];
                        }
                       
                    }
                }
                
            }
        }
        
        out(numrow);
        System.out.println("---------------------------------");
        out(numcol);
    }
    
    public static void out(int[][] arrs)
    {
        for(int i=0;i<arrs.length;i++)
        {
            for(int j=0;j<arrs[i].length;j++)
            {
                System.out.print(arrs[i][j]+" ");
            }
            System.out.println();
        }
    }
}
--------------------编程问答-------------------- 辛苦楼上的兄弟了,有其他办法吗,不遍历一次也可以。能得到正确结果即可 --------------------编程问答-------------------- 兄弟辛苦了。 --------------------编程问答-------------------- 刚测试过了。结果正确。谢谢哈
补充:Java ,  Java SE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,