Topic 1001:a+b for matrices

Source: Internet
Author: User

Title Description:

This is supposed to find a+b where A and B are both matrices, and then count the number of zero rows and columns .

Input:

The input consists of several test cases, each starts with a pair of positive integers M and N (≤10) which is the number of rows and columns of the matrices, respectively. Then 2*m lines follow, each contains N integers in [ -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 being processed.

Output:

For each test case, you should output on one line, the total number of zero rows and columns of a+b.

Sample input:
2 21 11 1-1-110 92 31 2 34 5 6-1 -2-3-4-5-60
Sample output:
15
Here is the simplest solution, but the complexity of the program itself is not high, so you can pass:
1 ImportJava.util.Scanner;2  3  Public classmain{4      Public Static voidMain (string[] args) {5Scanner in=NewScanner (system.in);6         intM,n;7m=in.nextint ();8          while(m!=0){9n=in.nextint ();Ten in.nextline (); One             int[][]a=New int[m][n]; A             int[][]b=New int[m][n]; -              for(inti=0;i<m;i++){ -                  for(intj=0;j<n;j++){ thea[i][j]=in.nextint (); -                 } - in.nextline (); -             } +              for(inti=0;i<m;i++){ -                  for(intj=0;j<n;j++){ +B[i][j]=in.nextint () +A[i][j]; A                 } at in.nextline (); -             } -             intCout=0; -              for(inti=0;i<m;i++){ -                 Booleando{true; -                  for(intj=0;j<n;j++){ in                     if(b[i][j]!=0) -do{false; to                 } +                 if(bo) -cout++; the             } *              for(inti=0;i<n;i++){ $                 Booleando{true;Panax Notoginseng                  for(intj=0;j<m;j++){ -                     if(b[j][i]!=0) thedo{false; +                 } A                 if(bo) thecout++; +             } - System.out.println (cout); $m=in.nextint (); $         } -     } - } the /************************************************************** - problem:1001Wuyi user:0000h the Language:java - result:accepted Wu time:930 Ms - memory:18872 KB About ****************************************************************/

Topic 1001:a+b for matrices

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.