1. 程式人生 > >A+B for Matrices(JAVA)

A+B for Matrices(JAVA)

題目描述:

    This time, you are supposed to find A+B where A and B are two matrices, and then count the number of zero rows and columns.

輸入:

    The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which are the number of rows and columns of the matrices, respectively. Then 2*M lines follow, each contains N integers in [-100, 100], separated by a space. The first M lines correspond to the elements of A and the second M lines to that of B.

    The input is terminated by a zero M and that case must NOT be processed.

輸出:

    For each test case you should output in one line the total number of zero rows and columns of A+B.

樣例輸入:
2 2
1 1
1 1
-1 -1
10 9
2 3
1 2 3
4 5 6
-1 -2 -3
-4 -5 -6
0
樣例輸出:
1
5
來源:

PS:這題是從九度題庫裡找到的 。

最近在用java,於是使用java程式語言來實現。

正常的思路應該是這樣:

1、實現兩個矩陣的輸入,可以通過二維陣列實現。

這裡為了節省空間消耗,可以只適用一個數組,第二個陣列輸入時直接對應相加即可。

2、分別橫向和縱向對陣列遍歷,統計符合條件項的個數,並輸出。

程式碼:

import java.util.*;
public class Main
{
	 public static void main(String args[])
	    {
	    int M,N;
	    Scanner cin = new Scanner(System.in);
	    while(cin.hasNext())
	    {
	        int sum = 0;
	        M = cin.nextInt();
	        if(M==0)break;
	        N = cin.nextInt();
	        int A[][] = new int[M][N];
	        int B[][] = new int[M][N];
	        for(int i=0;i<M;i++)
	            for(int j=0;j<N;j++)
	            A[i][j] = cin.nextInt();
	        for(int i=0;i<M;i++)
	            for(int j=0;j<N;j++)
	            B[i][j] = cin.nextInt();
	        for(int i=0;i<M;i++)
	            for(int j=0;j<N;j++)
	            A[i][j] = A[i][j]+B[i][j];
	        for(int i=0;i<M;i++)
	        {
	        	int j=0;
	            for(;j<N;j++)
	            {
	                if(A[i][j] != 0)break;	                
	            }
	            if(j == N)sum++;
	        }
	        for(int i=0;i<N;i++)
	        {
	        	int j=0;
	            for(;j<M;j++)
	            {
	                if(A[j][i] != 0)break;
	            }
	            if(j == M)sum++;
	        }
	    System.out.println(sum);
	    }
	     
	     
	    }
	}

改進思路:

1、仍然是輸入。考慮用空間換時間,這裡增加一個一維陣列(M+N的大小),對應各列和各行,初始值為0,如果該列(行)有不為0的數值,該陣列項加一,由於可以放在第二個二維陣列輸入的迴圈裡,所以可以避免後期迴圈。

2、統計一維陣列中0值的大小。

程式碼:

mport java.util.*;
public class Main
{
    public static void main(String args[])
    {
    int M,N;
    Scanner cin = new Scanner(System.in);
    while(cin.hasNext())
    {
        int sum = 0;
        M = cin.nextInt();
        if(M==0)break;
        N = cin.nextInt();
        if(N==0)break;
        int A[][] = new int[M][N];
        int tmp[] = new int[M+N];
        for(int i=0;i<M;i++)
            for(int j=0;j<N;j++)
            A[i][j] = cin.nextInt();
        for(int i=0;i<M+N;i++)tmp[i]=0;
        for(int i=0;i<M;i++)
            for(int j=0;j<N;j++)
            {
            A[i][j] += cin.nextInt();
            if(A[i][j] !=0)
                {
                tmp[i]++;
                tmp[M+j]++;
                }
            }
        for(int i=0;i<M+N;i++)
            {
            if(tmp[i] == 0)sum++;
            }
    System.out.println(sum);
    }
     
     
    }
}
/**************************************************************
    Problem: 1001
    User: xjtuyelei
    Language: Java
    Result: Accepted
    Time:1700 ms
    Memory:17816 kb
****************************************************************/

問題結束,歡迎討論。