Buildings

ゞ 浴缸里的玫瑰 2023-07-16 12:58 58阅读 0赞

Problem Description

We divide the HZNU Campus into NM grids. As you can see from the picture below, the green grids represent the buidings. Given the size of the HZNU Campus, and the color of each grid, you should count how many green grids in the NM grids.
Input
Standard input will contain multiple test cases. The first line of the input is a single integer T which is the number of test cases. T test cases follow.
The first line of each test case contains two integers n and m(1<=n,m<=100), the size of the campus. Then follow n lines, each line containing m integers. The j-th integer in the i-th line is the color of that grid, 0 stands for white color, while 1 stands for green.
Output
Results should be directed to standard output. For each case, output an integers T, the total green grids in the N*M size campus.

Sample Input

  1. 2
  2. 2 2
  3. 1 1
  4. 0 0
  5. 3 3
  6. 1 0 1
  7. 0 0 1
  8. 1 1 0

Sample Output

  1. 2
  2. 5
  • 定义动态二维数组

方法一:使用new

  1. int m,n;
  2. cin>>m>>n;
  3. int **data=new int*[m];
  4. for(int i=0;i<m;i++){
  5. data[i]=new int[m];
  6. }
  7. for(int i=0;i<m;i++){
  8. for(int j=0;j<n;j++){
  9. cin>>data[i][j];
  10. }
  11. }

方法二:使用vector

  1. cin>>n>>m;
  2. vector<vector <int> >a(n);
  3. for(int i=0;i<n;i++){
  4. a[i].resize(m);
  5. }

Code:

  1. //定义动态二维数组,计算二维数组中1 的个数
  2. #include<iostream>
  3. using namespace std;
  4. int main() {
  5. int t,n,m,count;
  6. cin >> t;
  7. while (t != 0) {
  8. t--;
  9. count = 0;
  10. cin >> n >> m;
  11. if (n < 1 || n>100 || m < 1 || m>100) return 0
  12. int** data = new int* [n];//定义动态二维数组
  13. for (int i = 0; i < n; i++) {
  14. data[i] = new int[m];
  15. }
  16. for (int i = 0; i < n; i++) {
  17. for (int j = 0; j < m; j++) {
  18. cin >> data[i][j];
  19. if (data[i][j] == 1) count++;
  20. }
  21. }
  22. cout << count << endl;
  23. }
  24. }

发表评论

表情:
评论列表 (有 0 条评论,58人围观)

还没有评论,来说两句吧...

相关阅读

    相关 TASKING build

    TASKING 编译有图形化编译,还有一种是命令行编译。 图形化编译很简单,就是打开TASKING,然后build就行。 重点来说命令行编译。 命令行编译

    相关 eclispe build

    eclispe自动编译 由于eclipse 有自动编译功能,在往常的使用过程是一直未曾注意到这个问题一直以为是运行时编译. spring 注入失败 无法创建组件对象

    相关 Build

    public class Build \{ //当一个版本属性不知道时所设定的值。 public static final String UNKNOWN = "unknow