• 题目1001:A+B for Matrices


    题目描述:

        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

    下面是最简单的解法,不过程序本身复杂度不高,所以可以通过:
     1 import java.util.Scanner;
     2  
     3 public class Main{
     4     public static void main(String[] args){
     5         Scanner in=new Scanner(System.in);
     6         int M,N;
     7         M=in.nextInt();
     8         while(M!=0){
     9             N=in.nextInt();
    10             in.nextLine();
    11             int[][]a=new int[M][N];
    12             int[][]b=new int[M][N];
    13             for(int i=0;i<M;i++){
    14                 for(int j=0;j<N;j++){
    15                     a[i][j]=in.nextInt();
    16                 }
    17                 in.nextLine();
    18             }
    19             for(int i=0;i<M;i++){
    20                 for(int j=0;j<N;j++){
    21                     b[i][j]=in.nextInt()+a[i][j];
    22                 }
    23                 in.nextLine();
    24             }
    25             int cout=0;
    26             for(int i=0;i<M;i++){
    27                 boolean bo=true;
    28                 for(int j=0;j<N;j++){
    29                     if(b[i][j]!=0)
    30                         bo=false;
    31                 }
    32                 if(bo)
    33                     cout++;
    34             }
    35             for(int i=0;i<N;i++){
    36                 boolean bo=true;
    37                 for(int j=0;j<M;j++){
    38                     if(b[j][i]!=0)
    39                         bo=false;
    40                 }
    41                 if(bo)
    42                     cout++;
    43             }
    44             System.out.println(cout);
    45             M=in.nextInt();
    46         }
    47     }
    48 }
    49 /**************************************************************
    50     Problem: 1001
    51     User: 0000H
    52     Language: Java
    53     Result: Accepted
    54     Time:930 ms
    55     Memory:18872 kb
    56 ****************************************************************/
  • 相关阅读:
    Python:字母图形(蓝桥杯)
    Python:list列表中元素输出的几种情况
    Python:实现杨辉三角(蓝桥杯)
    Python:DataFrame转dict字典
    RedHat 5 Enterprise DHCP服务器的安装与配置(Windows验证)
    Python时间模块。
    django项目中form表单和ajax的文件上传功能。
    django项目后台权限管理功能。
    django项目中cxselect三级联动
    django项目用higcharts统计最近七天文章点击量。
  • 原文地址:https://www.cnblogs.com/qq1029579233/p/4418104.html
Copyright © 2020-2023  润新知