Advertisement
dyamondz

Fila amb més 1's - X66680

Dec 30th, 2017
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. typedef vector<vector<int> > Matrix;
  6.  
  7. // input : a matrix NxM, N>0, M>0, containing sorted rows of only 0's and 1's.
  8. // output: index (between 0 and N-1) of the first row with maximum number of 1's)
  9. int find_row_most_ones(const Matrix& M){
  10.     int row=0,aux,max=0;
  11.     for(int i=0;i<M.size();++i){
  12.         aux=0;
  13.         for(int j=0;j<M[0].size();++j){
  14.             if(M[i][j]==1) ++aux;
  15.         }
  16.         if(aux>max){
  17.             max=aux;
  18.             row=i;
  19.         }
  20.     }
  21.     return row;
  22. }
  23.  
  24. int main(){
  25.     int n,m;
  26.     while(cin>>n){
  27.         cin>>m;
  28.         Matrix p(n,vector<int>(m));
  29.         for(int i=0;i<n;++i){
  30.             for(int j=0;j<m;++j) cin>>p[i][j];
  31.         }
  32.         cout<<"Row: "<<find_row_most_ones(p)<<endl;
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement