1729 - Find the rows with most 1's

通过次数

0

提交次数

0

时间限制 : 1 秒 内存限制 : 128 MB

Given a 2D matrix, which contains only 0's and 1's, find the first row with most 1's.

题目输入

多组测试。每一组第一行是行数M和列数N. It follows M lines, each having N numbers (only 0's and 1's). There is a space between every 2 numbers.


题目输出

每组输出结果占一行


输入/输出样例

输入格式

2 3

1 0 1

0 0 1

3 4

1 0 0 0

1 1 0 0

0 0 0 0

0 0 0 1

输出格式

0
1

C语言解答

#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
int main()
{
    int a,b,j,k,t,m;
    int *pb;
    while(scanf("%d%d",&a,&b)!=EOF)
    {
        if(a<2) continue;
        j=0;
        k=0;
        t=-1;
        m=0;
         pb=(int*)malloc(b*sizeof(int));
         if(pb==NULL)
         exit(-1);
         while(a--)
         {
             t++;
             k=0;
             int i;
             for(i=0;i<b;i++)
             scanf("%d",pb+i);
             for(i=0;i<b;i++)
             if(*(pb+i)==1)
             k++;
             if(k>j)
             {
                 j=k;
                 m=t;
             }

         }
         printf("%d\n",m);
         free(pb);
    }



    return 0;
}

C++解答

#include<iostream>
#include<iomanip>
#include<fstream>
#include<string> 
#include<climits>
#include<cctype>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
int main()
{
//	ifstream cin("aaa.txt");
   int x,a[100000]; 
   int m,n,i,j,max,count;
	
	while(cin>>m>>n)
	{
		max=-1;
		for(i=0;i<m;i++)
		{
 			count=0;
			for(j=0;j<n;j++)
			{
				cin>>x;
				if(x) count++;
			}
			a[i]=count;
			if(count>max) 
		       max=count;
		}
		for(i=0;i<m;i++) if(max==a[i]) {cout<<i<<endl;break;}
	}
		

	
	
	
     return 0;
}